Perform Trim() while using Split()

C#StringTrim

C# Problem Overview


today I was wondering if there is a better solution perform the following code sample.

string keyword = " abc, foo  ,     bar";
string match = "foo";
string[] split= keyword.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string s in split)
{
  if(s.Trim() == match){// asjdklasd; break;}
}

Is there a way to perform trim() without manually iterating through each item? I'm looking for something like 'split by the following chars and automatically trim each result'.

Ah, immediatly before posting I found

List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();

in https://stackoverflow.com/questions/1728303/how-can-i-split-and-trim-a-string-into-parts-all-on-one-line

Still I'm curious: Might there be a better solution to this? (Or would the compiler probably convert them to the same code output as the Linq-Operation?)

C# Solutions


Solution 1 - C#

Another possible option (that avoids LINQ, for better or worse):

string line = " abc, foo  ,     bar";
string[] parts= Array.ConvertAll(line.Split(','), p => p.Trim());

However, if you just need to know if it is there - perhaps short-circuit?

bool contains = line.Split(',').Any(p => p.Trim() == match);

Solution 2 - C#

var parts = line
    .Split(';')
    .Select(p => p.Trim())
    .Where(p => !string.IsNullOrWhiteSpace(p))
    .ToArray();

Solution 3 - C#

I know this is 10 years too late but you could have just split by ' ' as well:

string[] split= keyword.Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);

Because you're also splitting by the space char AND instructing the split to remove the empty entries, you'll have what you need.

Solution 4 - C#

If spaces just surrounds the words in the comma separated string this will work:

var keyword = " abc, foo  ,     bar";
var array = keyword.Replace(" ", "").Split(',');
if (array.Contains("foo"))
{
Debug.Print("Match");
}

Solution 5 - C#

I would suggest using regular expressions on the original string, looking for the pattern "any number of spaces followed by one of your delimiters followed by one or more spaces" and remove those spaces. Then split.

Solution 6 - C#

Try this:

string keyword = " abc, foo  ,     bar";
string match = "foo";
string[] split = Regex.Split(keyword.Trim(), @"\s*[,;]\s*");
if (split.Contains(match))
{
    // do stuff
}

Solution 7 - C#

You're going to find a lot of different methods of doing this and the performance change and accuracy isn't going to be readily apparent. I'd recommend plugging them all into a testing suite like NUnit in order both to find which one comes out on top AND which ones are accurate.

Use small, medium, and large amounts of text in loops to examine the various situations.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncitronasView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#Darius KucinskasView Answer on Stackoverflow
Solution 3 - C#Miklos KanyoView Answer on Stackoverflow
Solution 4 - C#Jens GranlundView Answer on Stackoverflow
Solution 5 - C#MikeWView Answer on Stackoverflow
Solution 6 - C#Rubens FariasView Answer on Stackoverflow
Solution 7 - C#McAdenView Answer on Stackoverflow