Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a character \ in java

Tags:

java

I just wanted to replace sequence of a character '\' (i.e. consecutive like \\\) to single '\' in Java. If possible I dont want to iterate with for loop all. Is there anything possible with regex to find and replace? Because in my case I will get lots of input like this if user gave in application for each and every time I have to validate and replace all \ character.

Can anyone help me in this context.

input is : \\\\Program Files\\Microsoft Games\\\\\Chess\\\Chess.exe

output which I expect is : \Program Files\Microsoft Games\Chess\Chess.exe

like image 224
Bhuvanesh Waran Avatar asked Mar 09 '26 08:03

Bhuvanesh Waran


1 Answers

static String replaceConsecutiveBackslash(String input) {
    return input.replaceAll("\\\\{2,}", "\\\\");
}
like image 101
shmosel Avatar answered Mar 11 '26 23:03

shmosel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!