Tati della traduzione how to remove spaces at the beginning and end of the string

Hi to all,
I have a small problem, trivial, but annoying.
I have read similar posts, but I don’t think I have found the solution for me.
I have to remove the spaces at the beginning and end of a string, but, leave the ones in between.
ex: " aaaa bbbb "
I would need: “aaaa bbbb”

Can anyone help me?
thanks
Aaron

HI @AaronMark

You can try with Trim

" aaaa bbbb ".Trim

image

Regards
Gokul

Hi @AaronMark

You can also try with Regex expression

System.Text.RegularExpressions.Regex.Replace(" aaaa bbbb ","^\s|\s$","").ToString

image

image

Regards
Gokul

Hi Goku, this will remove all spaces at the beginning and end, or just 1 space.
That is, if there are 2, 5, 10 blank characters, does it strip them all?

Yes, It will trim all space starting and End of the string @AaronMark

This will trim only on space starting and End of the string

Regards
Gokul

it works!
Thanks Goku!

1 Like

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