How to remove a single letter word in a string?

Hi guys,

I would like to remove a single letter word from a string. For example,
the string is -
string1 = “United Parcel E Service”
Output string needs to be - “United Parcel Service”
(the letter ‘e’ must stay in ‘United’, ‘Parcel’ and ‘Service’ and must not be removed)

Another example would be
string2 = “MARTIN N EVANS”
Output needs to be - “MARTIN EVANS”

Hi Acrom,

If you want to remove only single letters,
Please assign `your_string= System.Text.RegularExpressions.Regex.Replace(your_string,“\b\D\s\b”,“”)

If you want to remove single letters and single digits, please use this regex
System.Text.RegularExpressions.Regex.Replace(your_string,“\b\w\s\b”,“”)`

For reference, please check this workflow.Remove_Single_letter.xaml (7.2 KB)

Best Regards,
Nimin

2 Likes

Hi Nimin,

Thank you for your reply. Yes it works for a single letter between a string. But this regex expression does not work for a single letter that is at the end of the string.

For example as similar as i had mentioned,
string1 = “United Parcel Service E”, the output needs to be “United Parcel Service”. The regex does not seem to work for this condition.

So the single letter which stands alone can be anywhere in the string, at the beginning or middle or at the end. I would like to remove that only.

What if you change "\b\D\s\b" to use an ‘or’
something like this pattern:
"(?<=(^|\s))\D(\s|$)"

I used “^|\s” so it looks for either the start of the string or a whitespace. “?<=” is a look behind so it only matches with the space after the letter (so when you replace it with “” it won’t remove the space. Then, used “\s|$” so it matches either a whitespace or the end of the line after the character.

By the way, I have not tested this pattern.

Regards.

2 Likes

Oops… Sorry, I missed that possibility…
@ClaytonM you are right. :+1: :grinning:

@nimin No worries ! Thank you to both of you. @ClaytonM Yep this is the way to go. I have tested it out and it works !

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