ITPub博客

首页 > > > java中最容易犯错的特殊字符

java中最容易犯错的特殊字符

作者:zhmw 时间:2019-12-27 10:26:27 0 删除 编辑

问题背景

能准确说出下面的java 执行完毕后会打印出什么?

System.out.println(

String.class.getName()+ ".class");

System.out.println(

String.class.getName().

replaceAll(".","/") + ".class");

相信对于第一行,大部分人不会犯错,打印

java.lang.String.class

我们想使用/去分割类的包,期待打印的结果为

java/lang/String/class

真实返回的结果是这个样子的:

////////////////.class

为什么会这样呢

 原因

问题在于String.replaceAll 接受了一个正则表达式作为它的第一个参数,而并

非接受了一个字符序列字面常量。(正则表达式已经被添加到了Java 平台的1.4

版本中。)正则表达式“.”可以匹配任何单个的字符,因此,类名中的每一个

字符都被替换成了一个斜杠,进而产生了我们看到的输出。

解决方式

方式一:使用转义字符

System.out.println(

String.class.getName().

replaceAll("\\.","/") + ".class");

 

打印结果

java/lang/String.class

是不是有点不懂,为什么会有两个?

第一个"\"代表的是引用(正则表达式中的Quotation),第二个代码"\"转义

Quotation

\Nothing, but quotes the following character

\QNothing, but quotes all characters until \E

\ENothing, but ends quoting started by \Q

方式二 使用Quotation

System.out.println(

String.class.getName().

replaceAll("\\Q.\\E","/") + ".class");

结果也是

java/lang/String.class

也可以使用

System.out.println(

String.class.getName().

replaceAll(Pattern.quote("."),"/") + ".class");

其内部实现也是使用Quotation

/** * Returns a literal pattern <code>String</code> for the specified

* <code>String</code>.

*

* <p>This method produces a <code>String</code> that can be used to

* create a <code>Pattern</code> that would match the string

* <code>s</code> as if it were a literal pattern.</p> Metacharacters

* or escape sequences in the input sequence will be given no special

* meaning.

*

* @param s The string to be literalized

* @return A literal string replacement

* @since 1.5

*/publicstatic String quote(String s) {

intslashEIndex = s.indexOf("\\E");

if(slashEIndex == -1)

return"\\Q" + s + "\\E";

StringBuilder sb =newStringBuilder(s.length() * 2);

sb.append("\\Q");

slashEIndex = 0;

intcurrent = 0;

while((slashEIndex = s.indexOf("\\E", current)) != -1) {

sb.append(s.substring(current, slashEIndex));

current = slashEIndex + 2;

sb.append("\\E\\\\E\\Q");

}

sb.append(s.substring(current, s.length()));

sb.append("\\E");

return sb.toString();

}

 

常见的特殊字符有:

EscapeSequence:

\ b (backspace BS, Unicode \\u0008)

\ t (horizontal tab HT, Unicode \\u0009)

\ n (linefeed LF, Unicode \\u000a)

\ f (form feed FF, Unicode \\u000c)

\ r (carriage return CR, Unicode \\u000d)

\ " (double quote ", Unicode \\u0022)

\ ' (single quote ', Unicode \\u0027)

\ \ (backslash \, Unicode \\u005c)

OctalEscape (octal value, Unicode \\u0000 to \\u00ff)

还有

Twelve tokens, formed from ASCII characters, are the separators (punctuators).

( ) { } [ ] ; , . ... @ ::

也可以使用下面的方法进行判断

import java.util.regex.Matcher;import java.util.regex.Pattern;

publicclass CheckSpecialCharacterString {

/** * Check whether the each character of String is special character or not using java

* @author www.instanceofjava.com

*/publicstaticvoid main(String[] args) {

String Str="Java String interview questions*$%";

String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";

for(inti = 0; i < Str.length(); i++) {

if (specialCharacters.contains(Character.toString(Str.charAt(i))))

{

System.out.println(Str.charAt(i)+": is a special character");

}

}

}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69901620/viewspace-3120460/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: k8s architecture
请登录后发表评论 登录
全部评论

注册时间:2018-12-04

  • 博文量
    295
  • 访问量
    9531