166

I want to split this line:

string line = "First Name ; string ; firstName";

into an array of their trimmed versions:

"First Name"
"string"
"firstName"

How can I do this all on one line? The following gives me an error "cannot convert type void":

List<string> parts = line.Split(';').ToList().ForEach(p => p.Trim()); 
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 2
    The error is returned because `ForEach` works on current instance of `IEnumerable` and returns `void`. – Majkel Nov 13 '09 at 10:13

9 Answers9

378

Try

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

FYI, the Foreach method takes an Action (takes T and returns void) for parameter, and your lambda return a string as string.Trim return a string

Foreach extension method is meant to modify the state of objects within the collection. As string are immutable, this would have no effect

starball
  • 20,030
  • 7
  • 43
  • 238
Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
27

The ForEach method doesn't return anything, so you can't assign that to a variable.

Use the Select extension method instead:

List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
20

After .net 5, the solution is as simple as:

string[] parts = line.Split(';', StringSplitOptions.TrimEntries);
Gui Ferreira
  • 4,367
  • 6
  • 28
  • 41
6

Because p.Trim() returns a new string.

You need to use:

List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();
Matt Breckon
  • 3,374
  • 20
  • 26
4

Here's an extension method...

    public static string[] SplitAndTrim(this string text, char separator)
    {
        if (string.IsNullOrWhiteSpace(text))
        {
            return null;
        }

        return text.Split(separator).Select(t => t.Trim()).ToArray();
    }
LawMan
  • 3,469
  • 1
  • 29
  • 32
2

Alternatively try this:

string[] parts = Regex.Split(line, "\\s*;\\s*");
Lawrence Phillips
  • 289
  • 1
  • 3
  • 12
  • This is a great idea, however, I believe that misses any trailing blanks at the end of the string, correct? – jrichview Jun 01 '17 at 18:13
2

try using Regex :

List<string> parts = System.Text.RegularExpressions.Regex.Split(line, @"\s*;\s*").ToList();
0

Split returns string[] type. Write an extension method:

public static string[] SplitTrim(this string data, char arg)
{
    string[] ar = data.Split(arg);
    for (int i = 0; i < ar.Length; i++)
    {
        ar[i] = ar[i].Trim();
    }
    return ar;
}

I liked your solution so I decided to add to it and make it more usable.

public static string[] SplitAndTrim(this string data, char[] arg)
{
    return SplitAndTrim(data, arg, StringSplitOptions.None);
}

public static string[] SplitAndTrim(this string data, char[] arg, 
StringSplitOptions sso)
{
    string[] ar = data.Split(arg, sso);
    for (int i = 0; i < ar.Length; i++)
        ar[i] = ar[i].Trim();
    return ar;
}
Levi C.
  • 41
  • 7
-1

Use Regex

string a="bob, jon,man; francis;luke; lee bob";
   String pattern = @"[,;\s]";
            String[] elements = Regex.Split(a, pattern).Where(item=>!String.IsNullOrEmpty(item)).Select(item=>item.Trim()).ToArray();;   
            foreach (string item in elements){
                Console.WriteLine(item.Trim());

Result:

bob

jon

man

francis

luke

lee

bob

Explain pattern [,;\s]: Match one occurrence of either the , ; or space character

Hung Vu
  • 5,624
  • 2
  • 30
  • 27