user4155786
user4155786

Reputation:

How do i remove all trailing space from word(both front and end white space) in comma seperated string

My string is like this:

Abc , xyz , pqr

Final output:

Abc,xyz,pqr

i want to remove all trailing space(from front and end) from my word whenever i encounter comma in my string but condittion is if my string contain comma or space.

Eg:

Abc pqr, ttt ooo

output:

Abc,pqr,ttt,ooo

(no space before or after the word)

Upvotes: 0

Views: 141

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460158

So all spaces and commas are separators and you want to remove all consecutive duplicates. You can use String.Split with StringSplitOptions.RemoveEmptyEntries and String.Join:

string[] parts = input.Split(new []{' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
string result = string.Join(",", parts);

This is my favourite since it's readable, maintainable and efficient. I've tested it against a regex approach with a 60000-length string and 1000 repetitions:

Regex:  11.68 seconds
String.Split + String.Join: 1.28 seconds

But if the string is very large you might want to use a StringBuilder approach.

This is the best result so far:

public static string SplitAnyKeepSingleSeparator(string input, string separator, params char[] delimiter)
{
    if(input == null) return null;
    input = input.Trim(delimiter);
    StringBuilder sb = new StringBuilder(input.Length);
    int index = 0;
    int delimiterIndex = input.IndexOfAny(delimiter);
    while (delimiterIndex != -1)
    {
        string token = input.Substring(index, delimiterIndex - index);
        sb.Append(token).Append(separator);
        index = delimiterIndex + 1;
        while (delimiter.Contains(input[index])) index++;
        delimiterIndex = input.IndexOfAny(delimiter, index);
    }
    sb.Append(input.Substring(index));
    return sb.ToString();
}

But with a 60,000 character-string it's still less efficient than the String.Split+Join approach.

Upvotes: 5

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

Try this one..

string input = "input:plumber, plumber output:plumber,,plumber";
input = input.Replace(" ", ",").Trim();
while (input.Contains(",,")) 
{
    input = input.Replace(",,", ",");
}

Edited...

I have tested the answers provided and compared with mine. (in VB.net)

Dim input As String = IO.File.ReadAllText("C:\Users\SARVESH\Desktop\abc.txt")
Dim stp As New Diagnostics.Stopwatch
stp.Start()
input = input.Replace(" ", ",").Trim()
While input.Contains(",,")
    input = input.Replace(",,", ",")
End While
stp.Stop()
MessageBox.Show(stp.ElapsedMilliseconds)

input = IO.File.ReadAllText("C:\Users\SARVESH\Desktop\abc.txt")
stp.Reset()
stp.Restart()
input = System.Text.RegularExpressions.Regex.Replace(input.Trim(" "c, ","c), "(?<=\b\w+\b)[\s,]+", ",")
stp.Stop()
MessageBox.Show(stp.ElapsedMilliseconds)

input = IO.File.ReadAllText("C:\Users\SARVESH\Desktop\abc.txt")
stp.Reset()
stp.Restart()
Dim parts As String() = input.Split({" "c, ","c}, StringSplitOptions.RemoveEmptyEntries)
Dim result As String = String.Join(",", parts)
stp.Stop()
MessageBox.Show(stp.ElapsedMilliseconds)

Tested on a string of 10616532 characters.

Tim Schmelter' code works much faster... does the task in 141 ms.

Mine does the same in 512 ms.

regex does the same in 1221 ms.

Upvotes: 0

Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10015

The simplest and the best in perfomance:

    private static string SplitWordsByComma(string s)
    {
        return Regex.Replace(s.Trim(' ', ','), @"(?<=\b\w+\b)[\s,]+", ",");
    }

fastest (and properly working for bound cases):

    private static string SplitWordsByComma(string s)
    {
        var sb = new StringBuilder(s.Length);
        for (int i = 0; i < s.Length; i++)
        {
            while (i < s.Length && !char.IsLetter(s[i]))
            {
                i++;
            }
            while (i < s.Length && char.IsLetter(s[i]))
            {
                sb.Append(s[i++]);
            }
            sb.Append(',');
        }
        return sb.Remove(sb.Length - 1, 1).ToString();
    }

Upvotes: 0

Miroslav Endyš
Miroslav Endyš

Reputation: 164

This should work :)

string inputStr = "ABC, cde , fgh, IJk";
string outputStr = inputStr.Replace(' ', '');

OR

string outputStr = string.Join(",",(inputStr.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries));

Upvotes: 0

Related Questions