C#切片/分配变量

时间:2016-10-06 07:28:48

标签: c# slice

        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#程序,它采用这样的值

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

Watson的姓氏,但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

3 个答案:

答案 0 :(得分:2)

使用String.Join

类似的东西:

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

答案 1 :(得分: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。

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

答案 2 :(得分:0)

获取ID-token的索引。

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));

同样使用索引获取姓氏和idNumber

        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;
        }
    }