on 09-17-2019 2:47 PM
Please share a groovy to replace all Special characters in a string, we can do the same in mapping as if now we have to replace some special characters but in future if we need to replace some other character means on that time we need add.
Thanks & Regards,
Dhanush.
Hi Dhanush
You can use the replaceAll method of the String class in Groovy. You pass it a regular expression and a replacement string. Here's a simple function using the method:
def String replaceSpecialChars(String toReplace, String replaceWith) {
toReplace.replaceAll(~/[^\w\s]/, replaceWith)
}
The regular expression replaces everything that is not:
You can update the regular expression as needed.
If you want to remove instead of replace, simply pass the empty string as the replacement value.
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.