How to use substring to remove leading quote mark

Hi Guys

I am trying to remove a leading quote mark from the start of a string. I have tried using substring but it is not working. I am not sure what I am doing wrong. Or if there is a better way of doing it.

String name = " "Score is positive "

name.substring(1).

I am trying to remove the starting quote mark " to get the following output:

“Score is Positive”

1 Like

Your example string is surrounded by quotes. The quick way is to do this:

name.Replace("""", String.Empty).Trim, which will remove whitespace surrounding the string as well. Remove the Trim part if you want to keep the whitespace.

The “safe” way to do this (i.e. without risking removing something other than quotation marks) is:

name = System.Text.RegularExpressions.Regex.Match(name.Trim, "^""", String.Empty). This will only remove the first character if it is the quotation marks character ". Again, if you want to keep the whitespace, remove the Trim portion, but be warned that if your string starts with a space, this will only remove the " if you leave the Trim portion on.

1 Like

Hi

Will you get " in all your strings and that too in starting of string?

YourString.TrimStart("""c)

1 Like

I prefer option 2. It looks safer. I tried to implement in UiPath but it is giving me errors with the word Matching. Do you need to install libraries to use RegEX in UiPath.

Hi Karthick, yes I get them in all my string and at the start. What does the c represent? I just do not want to remove actuall quote mark used in the string though like for sarcasm. Just the leading ones that seem to come through.

Converting to Chararray

Yes .

Using this will also replace starting " if at all you assign it…

If the value is in variable , I dont think it creates a problem.

1 Like

It did not work man.How do you remove the last quotes as well?

name.Replace("""", String.Empty).Trim will work for removing all quotes. If it isn’t working, there is something else wrong in the workflow. If this solution does not work, please share your workflow with proprietary data replaced with dummy data.

1 Like

@Anthony_Humphries Hi buddy. That works fine. But I do not want to remove all the quotes. I want to keep the one in the middle of the sentence, if it contains any. I just want to remove all leading one. It is the regex one that is not working.

YourString.TrimEnd("""c)

This will remove last " if any.

If the value is assigned to variable, we can use both trimstart and trimend to remove unwanted characters if any present .

1 Like

Ah, understood.

In that case, try this:

name = System.Text.RegularExpressions.Regex.Replace(name.Trim, "(^"")|(""$)", String.Empty).Trim

1 Like

It does not like the trim property. Is that necessary?

Hmm, let’s try handling the whitespace in the regex.

name = System.Text.RegularExpressions.Regex.Replace(name, "(^\s*"")|(""\s*$)", String.Empty)

2 Likes

Hi Anthony. That works. But I am just noticing the experssion output adds a “\r\n”. What is that?

That’s a carriage return. In Windows systems, anytime the Enter key is pressed to start a new line, a carriage return \r and newline \n is added to the end of the string. In Unix and Linux systems, only a \n is added to the end. These characters are invisible when reading text data, but a machine sees these characters and presents them in a way you can perform operations on them as though they were any other character on the keyboard.

1 Like

Thanks for your help @Anthony_Humphries and @karthick much appreciated. :slight_smile:

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