有没有办法重用字符串c#?

时间:2016-12-08 10:48:29

标签: c#

我在C#上仍然有点生疏 我需要存储大于文本文件中8个字母的单词。 这就是我现在所拥有的:

一个新的文本文件。

4 个答案:

答案 0 :(得分:8)

试试这个:

string[] words = fileString.Split(0x20) // split by space
IEnumerable<string> longerThan8 = words.Where(word => word.Length > 8);

答案 1 :(得分:2)

之类的东西替换整个东西怎么样?
foreach (var item in fileString.Split(' ').Where(t => t.Length > 7))
{
   Console.WriteLine(item);
}

答案 2 :(得分:1)

使用= ""重置它。使用StringBuilder会更好。

foreach (char c in fileString)
{ 
    newString += c;
    lettercount++;
    if (lettercount > 7 && c == ' ')
    {
        Console.WriteLine(newString);
        lettercount=0;
        newString = "";
    }
    if (c == ' ')
    {
        lettercount = 0;
        newString = "";
    }
}

如果您使用StringBuilder,则类似于:

foreach (char c in fileString)
{ 
    newString.Append(c);
    lettercount++;
    if (lettercount > 7 && c == ' ')
    {
        Console.WriteLine(newString.ToString());
        lettercount=0;
        newString.Clear();
    }
    if (c == ' ')
    {
        lettercount = 0;
        newString.Clear();
    }
}

答案 3 :(得分:0)

        foreach (string s in fileString.Split(' ').Where(_=>_.Length > 7))
        {
            Console.WriteLine(s);
        }