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;