Best way to remove line breaks and blank spaces from a string

Good afternoon people,
I would like to check with you what is the best way to remove line breaks and whitespace from a string.
I’ve tried several alternatives but I have not had success yet
I’ve exported my string to the Execution Log to see how it looks.

This is my String:
“\n---------------201900000020496\n---------------”

Hyphens “-” represent the whitespace
“\ n” represent line breaks

How UiPath Captures Element Text:
image

How the Element displays the text:
image

I tried treatments like LTRIM() and RTRIM() but it did not work

Can you give me an orientation?

5 Likes

@Rafaeloneil

Try this:

String Str = "\n 201900000020496\n "

            Str.Replace("\n","").Trim
1 Like

Hi buddy @Rafaeloneil
Instring = " \n 201900000020496\n"
Better one is this, the reason is it will remove all the newline in a paragraph and make them as single line…this can be used for a paragraph as well…
stringResult= Regex.Replace(instring, “\t|\n|\r”, “”)
Or
StringResult = instring.Replace(Environment.newLine,“”)

Hope this would help you buddy @Rafaeloneil
Cheers @Rafaeloneil

4 Likes

or if its just a space we can use trim method of string class buddy
like this if your get text is stored in a variable of name intext
then
intext = intext.ToString.Trim

Trim is a method that would trim both the trailing and leading empty spaces of a string
where the method that were used previously like LTRIM and RTRIM mentions LEFT TRIM and RIGHT TRIM…that would trim only in the specific direction like either before or after the string buddy, thats why that didn’t work
Fine this would work for sure or
Any issues still buddy @Rafaeloneil

3 Likes

@Rafaeloneil

If you want to trim

Before of the string then use: StringName.TrimStart

After the string then use: StringName.TrimEnd

Before/After or in between of the string then use: StringName.Trim

1 Like

were you able to trim out the blank space buddy @Rafaeloneil

You can use regular expression to remove whitespace characters.

\s matches any whitespace character (equal to [\r\n\t\f\v ])

New Regex("\s").Replace(inputString, "")

Dim InputString = vbCrLf & "        201900000020496" & vbCrLf & "                  "
Dim CleanedString = New Regex("\s").Replace(InputString, "")
2 Likes

Hi @Rafaeloneil,

So from your string you can remove new lines or line breaks (or)
you can just take the digits from your string.

Use the below expression, which will just take the digits from the input string and remove anything other than digits.

Regex.Replace(“\n---------------201900000020496\n---------------”,“[^\d]”,“”)

2 Likes

@Rafaeloneil, you simply use the Assign activity with your string. strMyString = strMyString.Replace(“\n”, “”).Trim()

1 Like

Hello Friend, yours and all the above suggestions worked, however besides treating the string I need to assign it to a cell of my DataTable:

When Assigning my treated string to a cell in my DataTable is a warning:
Cannot Assign from type ‘System.String’ to type ‘UiPath.Core.GerericValue’ in Assign Activity ‘Assign’

2 Likes

@Rafaeloneil, so you just modify the code with this
“strMyString = strMyString.tostring.Replace(”\n", “”).Trim()

When you use the Generic Value you have to change it’s type as String for string manipulations.

Hi buddy’s
I already solved the problem, my field in the table had to be changed to string instead of number, from that point the assing worked.
Many thanks to all for the help

4 Likes

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