String类型的常用

来源:互联网 发布:php文字特效代码 编辑:程序博客网 时间:2024/04/29 05:59

String 类型是java中重要的且实用的,今天使用了几个常用的方法;



public class StringTest{public static void main(String[] args){  String  hello="hello";  String  world="world";
                  //判断两个String类型字符是否相等;  if(hello.equals(world)){  System.out.println("true");  }else{  System.out.println("false");  }   //返回utf-16编码表示的给定字符串所需要的代码单元的数量;   int n=hello.length();   System.out.println(n);   //计算代码点的数量;   int cpCount=hello.codePointCount(0, hello.length());   System.out.println(cpCount);   char  first=hello.charAt(0);   char  last=hello.charAt(3);   System.out.println(first);   System.out.println(last);           3.//返回给定位置开始或结束的代码点           int a=hello.codePointAt(3);           System.out.println(a);           4.//判断字符串结尾   boolean f=hello.endsWith("o");   System.out.println(f);   5. //判断字符串是否相等   boolean  x=hello.equals("hello");   System.out.println(x);}}

运行结果:

false;

5;

5;

h;

l;

108;

true;

true;


   


0 0
原创粉丝点击