工作中遇到一段代码

1 private static String getClassNameWithoutPackage(Class cl) {
2     String className = cl.getName();
3     int pos = className.lastIndexOf(46) + 1;
4     if (pos == -1)
5       pos = 0;
6 
7     return className.substring(pos);
8   }

于是看jdk API,发现String.lastIndexOf(int ch),返回的值int index,返回指定字符在此字符串中最后一次出现处的索引。

参数:ch - 一个字符(Unicode 代码点)。即字符对应的ASCII码值或unicode值。包括 (0 和 0xFFFF)范围内的 ch 的值.

返回值:int index:字符最后一次出现的索引。

例:

1 String string= "abcdefghijklmnopqrstuvwxyz";
2 int index= string.lastIndexOf(97);
3 System.out.println("index="+index);

run :index=0;//即代码点97对应于ascII码表中的小写字母a。

相关方法:

String:

  public int codePointAt(int index){}

 作用:返回指定索引处的字符(Unicode 代码点)。索引引用 char 值(Unicode 代码单元),其范围从 0length() - 1

 参数:index - char 值的索引

返回值:index 处字符的代码点值

例:

1    String string= "abcdef";
2    int i=0;
3    int index2=-1;
4    while (i<string.length()) {
5          index2= string.codePointAt(i) ;
6          System.out.println("index2="+index2);
7      i++;
8    }

run:

index2=97
index2=98
index2=99
index2=100
index2=101
index2=102

即往方法codePointAt(int index)传入字符的index,返回字符串中对应字符的代码点。

例:

1 int ch= "你好吗".codePointAt(1);
2 System.out.println("ch="+ch);

run:ch=22909

回到文中最上面的代码,对应ascII码表,代码点46对应英文句点"."

posted on 2016-01-25 15:59  WesTward  阅读(1866)  评论(0编辑  收藏  举报