2016-10-06 76 views
0
 String input; 
     Console.WriteLine(":>"); 
     input = (Console.ReadLine()); 

     string[] column = input.Split(' '); 
     int number_of_elements = column.Count(s => s != null);//counts the number of elements inputted 

if (number_of_elements > 7 && column[0].ToLower() == "add") { 
      **String firstName = column[1, number_of_elements-6];** 
      String lastName = column[number_of_elements-5]; 
      String id_Clause = column[number_of_elements-4]; 
      String id_Number = column[number_of_elements-3]; 
      String as_Clause = column[number_of_elements-2]; 
      String as_Level = column[number_of_elements-1]; 

     } 

我试图做一个C#程序,需要这样的C#切片/分配变量

ADD Mary Jane Watson ID 123456 AS Advanced 

我试图让

  String firstName = Mary Jane; 
      String lastName = Watson; 
      String id_Clause = ID 
      String id_Number = 123456 
      String as_Clause = AS 
      String as_Level = Advanced 

姓氏沃特森,但之间的一切值ADD和Watson作为名字。

因此,如果输入的是

ADD Mary Jane Jennifer Watson ID 123456 AS Advanced 

那么结果将是

  String firstName = Mary Jane Jennifer; 
      String lastName = Watson; 
      String id_Clause = ID 
      String id_Number = 123456 
      String as_Clause = AS 
      String as_Level = Advanced 
+0

如果您的姓氏由两部分组成,该怎么办? IE _MAC DONALD_? – Steve

+0

备注 - 如果你只想使用非null元素,你最好过滤一次,并使用过滤列表。实际上,可能有8个非空元素,但不能保证,例如'number_of_elements - 3'将访问非空元素。 –

回答

2

使用String.Join

喜欢的东西:

firstName = String.Join(" ", column, 1, number_of_elements - 6); 
+0

他们想要跳过'column [0]'因为我们已经确定它是单词'add'而不是名字的一部分。 –

0

你可以使用.split( '')但由于您不知道名称的实际长度,并且您没有任何其他分隔符,因此很难。

一些事情可以做:

 String[] splitValues = "ADD Mary Jane Watson ID 123456 AS Advanced".Split(' '); 

     String lastName = splitValues[splitValues.Length-5] 
     String id_Clause = splitValues[splitValues.Length-4] 
     String id_Number = splitValues[splitValues.Length-3] 
     String as_Clause = splitValues[splitValues.Length-2] 
     String as_Level = splitValues[splitValues.Length-1] 

和名字取剩余值,跳过第一。这是ADD。

这只是一个例子,但我希望你明白这一点。此外,它不涵盖姓氏由多个部分组成的情况。

0

获取ID令牌的索引。

var s = input.Split(' '); 
var indexID = Array.FindIndex(s, a => a == "ID"); 

要得到名字,跳过ADD-token并取2小于索引(以避免姓和ID令牌)。

 string firstName = String.Join(" ", s.Skip(1).Take(indexID - 2)); 

同样使用索引来获得姓氏和身份识别

 string lastName = s[indexID - 1]; 
     string idNumber = s[indexID + 1]; 

水平是数组中的最后一项。

 string asLevel = s[s.Length - 1]; 

下面是一个完整的示例程序。

static void Main(string[] args) 
     { 
      string[] inputs = new string[] { "ADD Mary Jane Watson ID 123456 AS Advanced", "ADD Mary Jane Jennifer Watson ID 123456 AS Advanced" }; 

      foreach (string input in inputs) 
      { 
       Console.WriteLine(Extract(input).ToString()); 
      } 
     } 

     private static Person Extract(string input) 
     { 
      var s = input.Split(' '); 

      var indexID = Array.FindIndex(s, a => a == "ID"); 

      string firstName = String.Join(" ", s.Skip(1).Take(indexID - 2)); 
      string lastName = s[indexID - 1]; 
      string idNumber = s[indexID + 1]; 
      string asLevel = s[s.Length - 1]; 

      return new Person() 
      { 
       FirstName = firstName, 
       LastName = lastName, 
       IDNumber = idNumber, 
       ASLevel = asLevel 
      }; 
     } 


    } 

    class Person 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string IDNumber { get; set; } 
     public string ASLevel { get; set; } 

     public override string ToString() 
     { 
      return FirstName + " " + LastName + " " + IDNumber + " " + ASLevel; 
     } 
    }