Remove spaces before commas in string

Can someone please help me figure out how to remove just the spaces before the commas in a string?

Example:
“25 RIDGEWOOD RD ,SPRINGFIELD ,VT,051560000,015(SVC)”

Looking for:
"“25 RIDGEWOOD RD,SPRINGFIELD,VT,051560000,015(SVC)”

The values in the string between the commas are dynamic.

@macaskillh

Use this in assign

Str is where i assume the string is stored

str = Str.Replace(" ,",",")

Cheers

1 Like

Hi @macaskillh , what about string.replace(“ ,”, ”,”)? It replaces the space and the comma with just a comma.

1 Like

I apologize I pasted the wrong value in the example. Here is the correct one:

Example:
“25 RIDGEWOOD RD ,SPRINGFIELD ,VT,051560000,015(SVC)”

This is what I would be looking for:
“25 RIDGEWOOD RD,SPRINGFIELD,VT,051560000,015(SVC)”

@macaskillh

It looks the same…

But you can use the same provided above to remove space before comma

Cheers

Hi @macaskillh

You can also try on with Regex replace

System.Text.RegularExpressions.Regex.Replace(strvariable,”\s(?=\,)”,””)

Regards
Sudharsan

@macaskillh To remove space, just replace space with empty string.
str_input = str_input.Replace(" “,”")
removeSpaces.xaml (4.5 KB)

Hi @macaskillh

You can try with Regex Expression

System.Text.RegularExpressions.Regex.Replace("25 RIDGEWOOD RD ,SPRINGFIELD ,VT,051560000,015(SVC)","\s(?=,)","").ToString

image

Regards
Gokul

This removes the space(s) in the address also, I’m only looking to remove the spaces before the commas.

This works exactly as I need, IF I use the exact string but if I replace the string with my variable it only removes one space before the commas.

System.Text.RegularExpressions.Regex.Replace(PMRAddress,“\s(?=,)”,“”).ToString

For some reason when I paste my example the additional spaces are removed. I’ve included a screen shot below showing the variable with the multiple spaces.

image

@macaskillh

Please use like this

System.Text.RegularExpressions.Regex.Replace(PMRAddress,"\s+(?=,)","").ToString

This shpuld solve your issue

Cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.