简体   繁体   English

删除java字符串中的特殊字符

[英]remove special char in java string

i have a string like 我有一个字符串

String mydate = jan\\10 ; 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.. 所以我想用#,@等替换char \\。

how it is possible.. 怎么可能..

You must to shield slash: String mydate = "jan\\\\10" ; 你必须屏蔽斜线: String mydate = "jan\\\\10" ;

If you want to replace this char: 如果要替换此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. 你需要四个反斜杠的原因是第一个参数是一个正则表达式,它需要转义反斜杠,然后java期望字符串中的反斜杠也被转义,导致四个反斜杠。 Alternatively you could just declare your string like 或者你可以只声明你的字符串

String mydate = "jan\\10" ;

and have it print normally. 并正常打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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