2015-09-17 57 views
-3

我有一个字符串,它是一个句子。例如:C# - 用多个字符串之间的空格拆分字符串

string sentence = "Example sentence"; 

如何将此字符串分成多个字符串?所以:

string one = "Example"; 
string two = "sentence"; 
+4

你听说过String.Split'的'?你得到一个包含多个字符串的数组。您可以通过索引访问它们或使用循环来枚举它们。 –

回答

0

这将工作

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

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

这是一种欺骗,但你正在寻找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); 
     } 
    } 
} 

的。凡确保空字符串被跳过。