Reputation:
I have a string where its formatted with delimeters like:
string | time stamp in milliseconds | int counter | string
I need to be able to grab the counter value only. There will be a number of messages so the time stamp and counter will increase with each message. I am using a string builder for the incoming messages.
What is the best way to get the counter in each string message coming in?
sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
string content = sb.ToString();
Thank you
Upvotes: 0
Views: 80
Reputation: 5430
If string is coming like this:
string strContent = "abc|1399718438000|100|def";
You could use Split method
int iCounter = Convert.ToInt32(strContent.Split(new char[] {'|'})[2]);
Or preferably when dealing with single char for splitting a string.
int iCounter = Convert.ToInt32(strContent.Split('|')[2]);
Upvotes: 3
Reputation: 7036
Split is a little heavy here. Just get indexes of the second and the third separator and use SubString to get the counter part.
Upvotes: 0
Reputation: 904
You can also use this:
int counter = Convert.ToInt32(strContent.Split('|').ToList().ElementAt(2));
Upvotes: 0