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

时间: 2024-08-25 17:31:49

Sring类的codePointAt()方法的相关文章

java.lang.String 类的所有方法

java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点). int codePointBefore(int index) 返回指定索引之前的字符(Unicode 代码点). int codePointCount(int beginIndex, int endIndex) 返回此 String 的指定文本范围中的 Un

静态、抽象类、加载类、魔术方法等

静态  static关键字 普通成员普通成员是属于对象的 静态成员静态成员是属于类的 普通方法里面可以调用静态成员静态方法里面不能调用普通成员self关键字 在类里面代表该类 普通类 class Ren { public $name="张三"; public static $zhongzu; //静态成员 普通方法 function Say() { echo self::$zhongzu."你好"; } 静态类 static function Run() { ech

iOS:UIApplication类的OpenURL方法

1.调用app store界面方法 在实际开发中,往往要推荐自己其他应用和推荐自己的收费软件,那么我们就需要在程序中直接连接到app store的相应页面. 实际上的做法很简单,使用的还是UIApplication类的OpenURL方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"程序的相应连接"]]; 2.调用其它应用的方法 // 调用 自带mail [[UIApplicationsharedA

类的寻找方法

class A: def bar(self): print('bar') self.f1() class B(A): def f1(self): print('B') class C: def f1(self): print('C') class D(C,B): pass d1 = B() d1.bar() class C: def __init__(self): print('C的构造方法') self.ty = '动物' class A(C): def __init__(self): pri

Discuz论坛写出的php加密解密处理类(代码+使用方法)

PHP加密解密也是常有的事,最近在弄相关的东西,发现discuz论坛里的PHP加密解密处理类代码,感觉挺不错,在用的时候,要参考Discuz论坛的passport相关函数,后面我会附上使用方法,先把类代码帖上来: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php /*

访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)

访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: type TClassA = class procedure Proc; virtual; end; TClassB = class(TClassA) procedure Proc; override; end; TClassC = class(TClassB) procedure Proc; ove

关于JAVA的String类的一些方法

一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2.StringBuffer类的capacity()方法与String类的 length()的方法类似,但是她测试是分配给StringBuffer的内存空间的大小,而不是当前被使用了的内存空间. 3.如果想确定字符串中指定字符或子字符串在给定字符串的位置,可以用 indexOf()和lastIndexO

JavaGraphics类的绘图方法

Graphics类提供基本绘图方法,Graphics类提供基本的几何图形绘制方法,主要有:画线段.画矩形.画圆.画带颜色的图形.画椭圆.画圆弧.画多边形.画字符串等. 1. 画线段:在窗口中画一条线段,可以使用Graphics类的drawLine()方法: /** * 在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线 * * @param x1 * 第一个点的 x 坐标 * @param y1 * 第一个点的 y 坐标 * @param x2 *

UIBezierPath和CGContext类中的方法

 UIBezierPath和CGContext类中的方法 CGContextSetLineWidth(ctr, 10); // 即描写边线又填充 CGContextDrawPath(ctr, kCGPathFillStroke); void CGContextSetLineWidth(CGContextRef c, CGFloat width); // 设置边线的宽度 void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat