-3

I have a string which is a sentence. For example:

string sentence = "Example sentence";

How can I divide this string into multiple strings? So:

string one = "Example";
string two = "sentence";
Gaston
  • 189
  • 1
  • 4
  • 14
  • 4
    Have you heard of `String.Split`? You get an array which contains multiple strings. You can access them via index or use a loop to enumerate them. – Tim Schmelter Sep 17 '15 at 14:03

2 Answers2

3

This is a dupe but you are looking for string.Split (https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx) --

public class Program
{
    public static void Main(string[] args)
    {
        string sentence = "Example sentence";
        string[] array = sentence.Split(' ');

        foreach (string val in array.Where(i => !string.IsNullOrEmpty(i)))
        {
            Console.WriteLine(val);
        }
    }
}

The .Where ensures empty strings are skipped.

Tom
  • 2,360
  • 21
  • 15
1

This will work

string sentence = "Example sentence";
string [] sentenses = sentence.Split(' ');

string one = sentenses[0];
string two = sentenses[1];