Sring类的codePointAt()方法

工作中遇到一段代码

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对应英文句点"."

转载于:https://www.cnblogs.com/westward/p/5157722.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值