Does C# have a String Tokenizer like Java's?

前端 未结 11 1515
日久生厌
日久生厌 2020-12-01 11:31

I\'m doing simple string input parsing and I am in need of a string tokenizer. I am new to C# but have programmed Java, and it seems natural that C# should have a string tok

11条回答
  •  感情败类
    2020-12-01 12:28

    You could use String.Split method.

    class ExampleClass
    {
        public ExampleClass()
        {
            string exampleString = "there is a cat";
            // Split string on spaces. This will separate all the words in a string
            string[] words = exampleString.Split(' ');
            foreach (string word in words)
            {
                Console.WriteLine(word);
                // there
                // is
                // a
                // cat
            }
        }
    }
    

    For more information see Sam Allen's article about splitting strings in c# (Performance, Regex)

提交回复
热议问题