-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";
1
  • 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. Commented Sep 17, 2015 at 14:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

This will work

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.