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";
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";
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.
This will work
string sentence = "Example sentence";
string [] sentenses = sentence.Split(' ');
string one = sentenses[0];
string two = sentenses[1];