Question Remove Commas

JayWeb

Member
Joined
May 27, 2010
Messages
7
Programming Experience
1-3
Hi,

I was wondering how I could do the following:

If I have the string: Test,"Blah, Blah, Blah",Test1,Test2

I would like to remove the commas between the " " only.

So it would be like this: Test,"Blah Blah Blah",Test1,Test2
 
Hmm i guess that Regex Class (System.Text.RegularExpressions) will suit much better here but you can still use the String manipulation methods here.
So here we go:

VB.NET:
        Dim input As String = "Test,""Blah, Blah, Blah"",Test1,Test2"
        Dim BeforeQuotes As String = input.Substring(0, input.IndexOf(""""c))
        Dim BetweenQuotes As String = input.Substring(input.IndexOf(""""c), input.LastIndexOf(""""c) - input.IndexOf(""""c) + 1)
        Dim AfterQuotes As String = input.Substring(input.LastIndexOf(""""c) + 1, input.Length - (input.LastIndexOf(""""c) + 1))

        Console.WriteLine("Before quotes: " & BeforeQuotes)
        Console.WriteLine("Between quotes: " & BetweenQuotes)
        Console.WriteLine("After quotes: " & AfterQuotes)
        Console.WriteLine("Fixed BetweenQuotes: " & BetweenQuotes.Replace(","c, String.Empty))
        Console.WriteLine("All the portions Joined back: " & BeforeQuotes & BetweenQuotes & AfterQuotes)
 
You could also use TextFieldParser class, ReadFields method would return a String array of 4 fields from that string and you could manipulate the second field with String.Replace. If required you can String.Join the fields afterwards.
 
Hi kulrom,

Your solution worked great, although it turns out I need it to be able to do it more than once in a string.e.g.

Test, "Blah, Blah, Blah", Test1, Test2, Test3,"Test4, Test5"
to
Test, "Blah Blah Blah", Test1, Test2, Test3,"Test4 Test5"

I take it regex is my only option now?

Thanks

Jay
 
turns out I need it to be able to do it more than once in a string.e.g.
TextFieldParser will handle that also, since it's just another field.
You can also expand the String manipulations from before using IndexOf more times while you split up the string or specify start index.
Regex would also be capable of finding those string patterns, for this I would suggest Regex.Replace using a MatchEvaluator to replace the matched content.
 
Hi

I have put in the following code, although it isnt working right, I have not used regex very much and dont know how to fix it, can someone have a look for me.


VB.NET:
Dim text As String = "Test, ""Blah, Blah, Blah"", Test1, Test2, Test3,""Test4, Test5"""
Dim rx As New Regex("""[^""\r\n]*")
Dim result As String = rx.Replace(text, AddressOf removeCommas)


Function removeCommas(ByVal m As Match) As String
        Dim x As String = m.ToString()
        x = Replace(x, ",", "")
        Return x
End Function
 
You were simply missing the end quote " to match the quoted "string" << , expanded to VB code:
VB.NET:
Dim pattern As String = """[^""\r\n]*"""

Also, for removeCommas you can do this:
VB.NET:
Return m.Value.Replace(",", String.Empty)
If you also want to remove the field quotes, since with the field delimiter chars removed from field data they no longer need to be escaped, you can append this to the return expression:
VB.NET:
.Replace("""", String.Empty)
 
Back
Top