如何在stringbuilder中剪切一组字符?

时间:2012-01-03 11:17:47

标签: string stringbuilder

有没有办法在stringbuilder中分割一组字符 - 例如,如果我有“One Two Three Four”有一种获取每个单词的方法,然后我可以将它放入一个列表中(使用foreach循环)

3 个答案:

答案 0 :(得分:2)

我认为没有直接的方法将StringBuilder拆分为数组,首先需要转换为String,如下所示:

    StringBuilder sb = new StringBuilder();
    sb.append("One Two Three Four");
    String[] myArray = sb.toString().split(" ");

答案 1 :(得分:1)

for java

StringBuilder sb = new StringBuilder("One Two Three Four");
String[] words = sb.toString().split("[\\s]+");

答案 2 :(得分:0)

请尝试使用下面提到的代码。希望它可以帮助您完成任务:

    //Create Stringbuilder

        StringBuilder strb = new StringBuilder();
        strb.Append("One Two Three Four");

        // Convert Stringbuilder to string
        string a = strb.ToString();

        // Split the string based on seprator. Here seprator is space " "
        string[] str = a.Split();

        List<string> al1 = new List<string>();          

        // Add the word of splited string to a string List
        foreach (string str1 in str)
        {
            al1.Add(str1);
        }