LordTitiKaka
LordTitiKaka

Reputation: 2156

stringbuilder vs list string when searching line by line c#

I have an WinForm app that getting output from serail/telnet terminals because of historical decisions all output goes to a list like this

static List<string> BufferLog = new List<string>();

serialInputData += serialPort.ReadExisting();
BufferLog.Add(serialInputData);

Now I want to add another function to block thread until a sentence {one word is also possible } what I had in mind was to do something like

if (IsWaitForCustomMessage)
{
 while(IsNotTimeout)
 {

    List<string> waiterList = serialInputData.Split('\n').ToList();
    if (waiterList.Exists(x => x.Contains("SomeSentenc")) return true ;
 }
 return false;
} 

assuming that serialInputData is not containing one line but many lines

What I want to know is , Is there is any faster way to check those lines ?

The only other way to do it fairly simple for me is with stringBuilder , I am more the willing to try other ways

also fro your experiences should I change the BufferLog from List<string> to some other type ?

Upvotes: 0

Views: 408

Answers (1)

PhillipH
PhillipH

Reputation: 6222

Last question first - yes, I'd use StringBuilder instead of List(string) because its a closer fit to what you are doing (building a string with incremental inputs). Just tidier rather than better performance neccessarily.

I think you are asking how to wait until the StringBuilder contains a specific sequence of chars ? Instead of breaking it into lines, is there any reason you couldn't just use IndexOf ? This would prevent the need to move the strings around in memory and will be pretty fast.

Upvotes: 1

Related Questions