Gitz
Gitz

Reputation: 820

Split and add specific charcters to a string in C#

I am working with key-wording modules where each word end with , and remove spaces from the string example is given below:

if string is

one man sitting with girl ,  baby girl ,   Smiling girl 

then result should be

,one man sitting with girl,baby girl,Smiling girl,

I am trying this

  string[] strArray = str15.Split(new char[] { ',', ';' });
        if (strArray.Length >= 1)
            {
                foreach (string str3 in strArray)
                {
                    if (str3 != string.Empty)
                    {
                        strr1 = strr1 + str3 + ",";

                    }
                }
            }

But not able to remove spaces from string.

Upvotes: 0

Views: 212

Answers (4)

Greenhorn
Greenhorn

Reputation: 74

This should work:

        string test = "one man sitting with girl ,  baby girl ,   Smiling girl";
        Regex regex = new Regex(@"\s+,\s+");

        string result = regex.Replace(test, ",");

Upvotes: 0

suhaim
suhaim

Reputation: 314

Try this:

    var input = "one man sitting with girl ,  baby girl ,   Smiling girl";

    var output = string.Join(",", input.Split(',').Select(x => x.Trim()));

    output = string.Concat(",", output, ",");

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101701

Split your string and join it again by removing the white-spaces:

var input = "one man sitting with girl ,  baby girl ,   Smiling girl";

var output = string.Join(",", input.Split(',').Select(x => x.Trim()));

// If you wanna  enclose it with commas
output = string.Format(",{0},",output);

Upvotes: -1

Polynomial
Polynomial

Reputation: 28316

The first thing you want to do is tokenise the string, using Split().

string input = "some words   ,  not split , in a sensible    ,    way";   
string[] sections = input.Split(',');

This gives you an array of strings split by the comma delimiter, which would look something like this:

"some words   "
"  not split "
" in a sensible    "
"    way"

Now you want to trim those spaces off. The string class has a great little function called Trim() which removes all whitespace characters (spaces, tabs, etc) from the start and end of strings.

for (int i = 0; i < sections.Length; i++)
    sections[i] = sections[i].Trim();

Now you have an array with strings like this:

"some words"
"not split"
"in a sensible"
"way"

Next, you want to join them back together with a comma delimiter.

string result = string.Join(",", sections);

This gives you something along the lines of this:

"some words,not split,in a sensible,way"

And finally, you can add the commas at the start and end:

result = "," + result + ",";

Of course, this isn't the cleanest way of doing it. It's just an easy way of describing the individual steps. You can combine all of this together using LINQ extensions:

string result = "," + string.Join(",", input.Split(',').Select(s => s.Trim())) + ",";

This takes the input, splits it on the comma delimiter, then for each item in the list executes a lambda expression s => s.Trim(), which selects the trimmed version of the string for each element. This resulting enumerable is then passed back into string.Join(), and then we add the two commas at the start and end. It has the same function as the above steps, but does it one line.

Upvotes: 4

Related Questions