Feedback

Split string in seperate words

Sprache: C#

This code split a string in seperate words.
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "This is a big house";

            //Split string on spaces.
            string[] wordsSplit = s.Split(' ');

            foreach (string word in wordsSplit)
            {
                Console.WriteLine(word);
            }
        }
    }
}
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "This is a big house";

            //Split string on spaces.
            string[] wordsSplit = s.Split(' ');

            foreach (string word in wordsSplit)
            {
                Console.WriteLine(word);
            }
        }
    }
}

6 Kommentare

  1. This solution solves this problem not very well. So what is with double spaces (accidently) or tabs and newline between words? Regular Expressions are more recommended on this:
    [code]
    string words = „Lorem ipsumtdolor sit amet consetetur sadipscing elitr“;
    string[] wordsSplit = Regex.Split(words, @“s+“);
    [/code]
    There are several more problems to solve, e.g. to cope with special characters.