String类型的常用

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;


   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章