Is there a way to find the indexOf any special character if it appear within a string using linq and regex “\W”?
If the indexOf length is < 2 i’d like to remove the special character, else, keep it.
Can this be done?
Is there a way to find the indexOf any special character if it appear within a string using linq and regex “\W”?
If the indexOf length is < 2 i’d like to remove the special character, else, keep it.
Can this be done?
Here’s an example of how you can achieve this in UiPath:
Assign inputString = “Hello! World!”
Assign specialCharacter = “!”
inputString
and the Pattern property to "\W"
. This activity will give you a collection of Match objects.index
matches.FirstOrDefault().Index
Here, matches
refers to the collection of Match objects obtained from the Matches activity.index < 2
.Assign inputString = inputString.Remove(index, 1)
Note: Make sure to import the System.Linq and System.Text.RegularExpressions namespaces in the Imports panel of UiPath Studio to use LINQ and regular expressions.
By following these steps, you can find the index of a special character using LINQ and regex in UiPath and remove it from the string if the index is less than 2.
we can use
or working with regex and using the index property from match
Thanks @ppr is there a way that any special character can be detected? Anything that isn’t A-ZA-z0-9
yes we can do. So with the \W
you started will, So now use the index property for the evaluation and then replace if it is needed.
When furthe assistance is needed, just share some text samples with us
Thanks @ppr I’m struggling a bit with the syntax. At the moment i have:
strTest = “Bill-Bo”
intTest.IndexOf(“\W”)
The result of intTest is zero though, so it doesn’t seem to be finding the hyphen.
I’ve tried to play around with calling System.Text.RegularExpression.Regex but not sure now to combine with indexOf
No, IndexOf is not ready for RegexPattern
Ah of course! Thank you for solving
Why use Regex when IndexOf works just fine and is simpler?
Simply because I don’t want to specify every single special character. Much easier to use Regex to see if any special character exists in the string then decide what to do with it depending on its position