我有一个字符串,这是一个句子。例如:
string sentence = "Example sentence";
如何将此字符串分成多个字符串?所以:
string one = "Example";
string two = "sentence";
答案 0 :(得分:1)
这是一个骗局,但你正在寻找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);
}
}
}
.Where确保跳过空字符串。
答案 1 :(得分:0)
这将有效
string sentence = "Example sentence";
string [] sentenses = sentence.Split(' ');
string one = sentenses[0];
string two = sentenses[1];