简体   繁体   中英

remove special char in java string

i have a string like

String mydate = jan\\10 ;

but when i print this string i did n't get currect string. so i want replace the char \\ by any other char , like #,@ etc..

how it is possible..

You must to shield slash: String mydate = "jan\\\\10" ;

If you want to replace this char:

mydate = mydate.replace("\\\\", "#");//result is jan#10

String myDate = @"jan\10";
String newDate = myDate.replace('\\', '#');
String str = "hello\\world";
System.out.println(str.replaceAll("\\\\", "@"));

output:

hello@world
mydate.replaceAll("\\\\","#");

Will replace it. The reason you need four backslashes is that the first argument is a regular expression, which expects backslashes to be escaped, and then java expects the backslashes in strings to be escaped as well, leading to the four backslashes. Alternatively you could just declare your string like

String mydate = "jan\\10" ;

and have it print normally.

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