How to remove punctuation mark from a variable

Hi,

I assign to variable string and punctuation mark, eg. “,”.
Eg.strColour = strColour + row(colour) + ", "
I would like to cut last punctuation.

Eg. my string is:
yellow, black, red,
but finally I would like to receive:
yellow, black, red

1 Like

Well,

if you want to use only Uipath to do it (no VB code/Regex or .NET methods separately)

Then you can split the string using your comma s delimiter, then remove empty entries and then join them.

Or you can use regEx

Text = Regex.Replace(Text, @"\p{P}\r\n", "\r\n");

The above RegEx will remove punctuation at the end of every line (even if it a multi line text with new line character)

Regards :slight_smile:

2 Likes

Hi @Krzysztof,

You can use substring also to do that…

Text = Text.substring(0 , Text.Length -1)

Try this…

1 Like

@Krzysztof
Trim accepts any characters.

strColour = strColour.Trim(","c)

1 Like

Well,

The OP might want any and all punctuation marks to be trimmed…

@Krzysztof did the solution work?

Hi,

what does “c” in strColour.Trim(","c) mean? It does not work:(

@Krzysztof,

c represents the character here… Instead of trim, you need to use Split … Trim is for someother purpose

It works!!! Thank you!
Yould you explain, what does “0” mean? Beginning of the text?

The Trim method takes a char array as input.
“c” is shorthand for “.ToCharArray”.

If we start with the string “yellow, black, red,” and use the Trim method the output is “yellow, black, red”.
image
image

Substring(start, length) is the method. So 0 tells it to start at the beginning and Text.Length-1 tells it to take all but the very last character.

“0” is the starting index and Text.Length - 1 states that, trim till the length of the string - 1 as the last character you need to trim.

@Krzysztof

Hi,

I think that the solution from @HareeshMR is easiest for me.
I do not how to use Regex.Replace(Text, @"\p{P}\r\n", “\r\n”) in practice.

I suggested Trim because it more closely represents the actual logic that was originally requested. Substring will remove the last character without making sure it is a comma, which would lead to part of the string being removed if for some reason a comma wasn’t at the end.

1 Like

As I see it does not work becouse to string I add ", " (there is also space) - at the end of string I have space. It works when at the end is comma.

I can use substring(0 , Text.Length -2) and it cut space and comma.

@Krzysztof
This will get rid of the space and then the comma:
strColour = strColour.Trim.Trim(","c)

The first Trim with no parameters gets rid of whitespace and the second one gets rid of commas.

2 Likes

It works.

1 Like

Suit yourself buddy. Easy or difficult a solution nonetheless - glad that your issue is resolved. :slight_smile:

1 Like

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