重新開始第5天
阿新 • • 發佈:2020-09-23
String
String s1 = "hello world";
byte[] bytes = {97,98,99};
String s2 = new String(bytes);//將bytes陣列轉換成字串
String s3 = new String(bytes,1,2);
//位元組陣列,陣列元素下標的起始位置,長度
System.out.println(s3);
方法
public class ReplayTest {
public static void main(String[] args) {
//String類中常用方法
char c = "中國人".charAt(1);
System.out.println(c);//國
//int compareTo(String anotherString) 比較
int result = "abc".compareTo("abc");
System.out.println(result);//0(等於0)前後一致
int result2 = "abcd".compareTo("abce");
System.out.println(result2);//-1(小於0)前小後大
int result3 = "abce".compareTo("abcd");
System.out.println(result3);//1(大於0)前大後小
System.out.println("xyz".compareTo("yxz"));//-1
//boolean contains(CharSequence s) //判斷前面的字串中是否包含後面的子字串
System.out.println("HelloWorld.java".contains(".java"));//true
//包含為true不包含為false
//boolean endsWith(String suffix)
//判斷當前字串是否以某個字串結尾
System.out.println("tese.txt".endsWith(".java"));//false
System.out.println("tese.txt".endsWith(".txt"));//true
//boolean equals(Object anObject)
//比較兩個字串是否相等
System.out.println("abe".equals("abc"));//true
//boolean equalsIgnoreCase(String anotherString)
//判斷兩個字串是否相等,並且忽略大小寫
System.out.println("AbC".equalsIgnoreCase("abc"));//true
//byte[] getBytes()
//將字串物件轉換成位元組陣列
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length ; i++) {
System.out.println(bytes[i]);//97,98,99
}
//int indexOf(String str)
//判斷某個字串在當前字串中第一次出現的索引(下標)
System.out.println("abcdsafdsafd".indexOf("d"));//3
//boolean isEmpty()
//判斷某個字串是否為空
String s = "a";
System.out.println(s.isEmpty());//false
//int length()
//字串長度
//判斷陣列長度是length屬性,判斷段字串長度是length()方法
System.out.println("accd".length());//4
//int lastIndexOf(String str)
//判斷某個字串在當前字串中最後一次出現的索引(下標)
System.out.println("fjdafldbnaognoeiw".lastIndexOf("bn"));//7
//String replace(CharSequence target, CharSequence replacement)
//替換