Remove first word from a string

hi! i want to remove the first “and” from a string. sometimes it works, sometimes it doesnt when i do Confirmation2Banks.Substring(2).Trim, where Confirmation2Banks = Confirmation2Banks + " and " + BankNames.

is there a better solution for this that will allow me to always remove the first “and”?

please see this video to learn more about split strings. After splitting the string, you can delete the word you want.

Video: UiPath | Split Words | Split String | Split Function in UiPath | Get Words from String | Strings - YouTube

Best regards
Mahmoud

Hey, Aly! You can use Regex.Replace Method!

image

We create a regex replace where we first pass the Pattern which we want to replace. In .Replace we pass the string where we will replace, for which other string we will replace and the index of ocurrance.

Lets look the variables:

Here the output!
“Item 1, item and2, item and3”

Hope this helps!

hi i tried this method but it removed both “and” instead of first “and” only

output

Hi @aly

You can try this by changing to this pattern

Hope this Helps

Regards
Sudharsan

Hey Aly, check if the pattern its just “and”. Check if this assign is not inside a loop. Check the text right before then right after this assign.

This method only remove the ocurrence that you indicate. If this text don have sensitive information, send us the full text.

Try change the pattern to " and " too.

hi the assign is inside a loop and it has to be inside for the variable Confirmation2Banks to work. (same goes for other variables)

I’ve changed the pattern to " and" instead of " and " because this will be the output, whereby the 2 banks aren’t separated with a spacing.

Hey Aly, I think what might be happening is that Replace might be called multiple times for your text (perhaps by loop), in which case it’s taking out all the “ands” 1 to 1.
I believe that debugging a little, you can make the method only run once in your text.

Tell me if you’ve reached the desired solution!

Regards

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