Edward Tanguay
Edward Tanguay

Reputation: 193352

How can I split and trim a string into parts all on one line?

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()); 

Upvotes: 171

Views: 137185

Answers (10)

Dmitry Shashurov
Dmitry Shashurov

Reputation: 1724

Another possible single-line option avoids LINQ:

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

Upvotes: 0

Gui Ferreira
Gui Ferreira

Reputation: 4637

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

string[] parts = line.Split(';', StringSplitOptions.TrimEntries);

Upvotes: 24

C&#233;dric Rup
C&#233;dric Rup

Reputation: 15938

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

Upvotes: 385

user1074149
user1074149

Reputation:

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;
}

Upvotes: 0

Hung Vu
Hung Vu

Reputation: 5924

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

Upvotes: -1

user2826608
user2826608

Reputation: 29

try using Regex :

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

Upvotes: 2

LawMan
LawMan

Reputation: 3625

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();
    }

Upvotes: 4

Lawrence Phillips
Lawrence Phillips

Reputation: 289

Alternatively try this:

string[] parts = Regex.Split(line, "\\s*;\\s*");

Upvotes: 2

Guffa
Guffa

Reputation: 700562

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();

Upvotes: 27

Matt Breckon
Matt Breckon

Reputation: 3374

Because p.Trim() returns a new string.

You need to use:

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

Upvotes: 6

Related Questions