简体   繁体   中英

Java remove escape characters from string

I have a string "http:\\/\\/i.ytimg.com\\/vi\\/0vxOhd4qlnA\\/maxresdefault.jpg" .

I want it like "http://i.ytimg.com/vi/0vxOhd4qlnA/maxresdefault.jpg" .

I tried almost everything available on stackoverflow to remove backslash or escape character, but nothing is working. I tried :

1) .replace("\\","")
2) .replaceAll("\\","")
3) .replace("\\\\/","/")
4) .replaceAll("\\\\/","/")

Thanks in advance! Manali

If you building the http string you can do as follows

System.out.println(httpString.replace('\\', '/'));

or if you want to get the http address from user use

        String httpString = "";
        Scanner scanner = new Scanner( System.in );
        System.out.print( "Enter the improper URL: " );
        httpString = scanner.nextLine();
        System.out.println(newString.replace('\\', '/'));

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