简体   繁体   中英

Is there a way of reusing a string c#?

I'm still a bit rusty on C# I need to store words greater that 8 letters from a text file. This is what I have at the moment:

na new text file.

Try this :

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

How about you replace the entire thing with something like

foreach (var item in fileString.Split(' ').Where(t => t.Length > 7))
{
   Console.WriteLine(item);
}

Use = "" to reset it. Using StringBuilder would be better though.

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

If you used StringBuilder , it would be something like:

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();
    }
}
        foreach (string s in fileString.Split(' ').Where(_=>_.Length > 7))
        {
            Console.WriteLine(s);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM