简体   繁体   中英

How to replace a character \ in java

I just wanted to replace sequence of a character '\\' (ie 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

static String replaceConsecutiveBackslash(String input) {
    return input.replaceAll("\\\\{2,}", "\\\\");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM