Remove the last 2 characters of a string when the 2nd to last character is a space

Hello, Hoping for some assistance.

I need to remove the last 2 characters of a string, only if the 2nd to last character is a space.

Doe, John J
Doe, Jonathan K

Need these to return as
Doe, John
Doe, Jonathan

The last character could be any of the 26 letters in the alphabet and the amount of characters before this could be any number.

Essentially, I need to find the last space and if there is only 1 letter following it, remove that space and letter from the string. Any ideas would be appreciated.

1 Like

Hi @dmdixon75,

Please assign, string_str=System.Text.RegularExpressions.Regex.Replace(your_string,"\s\w$","").Trim
Warm regards,

Thank you Nimin. I will try this out.

I did figure out a different way as well. I used substring to return the last 2 characters of the string and then did a check if the substring starts with a space. This ended up giving me the result I needed. But I will definitely work with yours as well to see which one works best. I appreciate the response.

1 Like

Hi @dmdixon75

Hope this will help you,