s.length():返回字符串的長度java
String s = "abcdefg";//7 System.out.println(s.length());
s.charAt(int index) :返回某索引處的字符數組
index屬於數組長度範圍測試
String s = "abcdefg"; System.out.println(s.charAt(3));//d
s.isEmpty():判斷是不是空字符串編碼
String s = "abcdefg"; System.out.println(s.isEmpty());//false
s.toLowerCase():將String中的全部字符轉換爲小寫code
String s = "ABCDEFG"; System.out.println(s.toLowerCase());//abcdefg
s.toUpperCase():將String中的全部字符轉換爲大寫排序
String s = "abcdefg"; System.out.println(s.toUpperCase());//ABCDEFG
s.trim():返回字符串副本,忽略前導空白和尾部空白繼承
String s = " ASDF GJ "; System.out.println(s.trim());//ASDF GJ
equals():比較字符串的內容是否相等索引
s1.equalsIgnoreCase(s2):忽略大小寫比較二者是否相等接口
String s1 = "asdfg"; String s2 = "ASDFG"; System.out.println(s1.equalsIgnoreCase(s2));//true
s1.concat("s2"):將指定字符串鏈接到此字符串的結尾,至關於用「+」內存
String s1 = "qwert"; System.out.println(s1.concat("+mnb"));//qwert+mnb
s1.compareTo(s2):比較兩個字符串的大小(不管哪一種方式建的字符串均可以)
String s1 = "asd"; String s2 = "cvb"; System.out.println(s1.compareTo(s2));//-2
String s2 = s16.substring(int index):返回一個新的字符串,它是此字符串從index開始截取到最後一個字符串
String s1 = "克里斯馬殷閣下"; System.out.println(s1.substring(4));//殷閣下
String s2 = s1.substring(int beginIndex, int endIndex):返回一個新的字符串,它是從beginIndex開始截取到endIndex(不包含)的一個子字符串(左閉右開)
String s1 = "克里斯馬殷閣下"; System.out.println(s1.substring(0,4));//克里斯馬
boolean b = s1.endsWith(String suffix) :測試此字符串是否以指定的後綴結束
String s = "asdfg"; System.out.println(s.endsWith("g"));//true
boolean b = s1.startsWith(String suffix):測試此字符串是否以指定的前綴開始
String s = "asdfg"; System.out.println(s.startsWith("a"));//true
boolean b = s.startsWith(prefix,toffset):測試此字符串從指定的索引開始的子字符串是否與指定前綴開始
String s = "asdfg"; System.out.println(s.startsWith("df", 2));//true
s1.contains(s2):此字符串包含指定的char值序列時,返回true
String s = "asdfg"; System.out.println(s.contains("sdf"));//true System.out.println(s.contains("sf"));//false
s.indexOf(String str):返回指定子字符串中第一次出現處的索引,不存在出現-1
String s = "asdfg"; System.out.println(s.indexOf("d"));//2 System.out.println(s.indexOf("b"));//-1
s.indexOf(str,fromIndex):返回指定子字符串在此字符串中第一次出現的索引,從fromIndex開始查找
String s = "asdfgasdfg"; System.out.println(s.indexOf("d", 5));//7
s.lastIndexOf(String str):返回指定的子字符串在此字符串中最右邊出現處的索引(仍是從前向後找,返回的索引也是從前日後數的索引)
String s = "asdfgasdfg"; System.out.println(s.lastIndexOf("f"));//8
str.lastIndexOf(str,fromIndex):返回指定的子字符串中最後一次出現處的索引,從指定的索引開始反向搜索(返回的索引值仍是從前日後找獲得的索引)
String s = "asdfgasdfg"; System.out.println(s.lastIndexOf("g", 5));//4
s.replace(oldchar,newchar):返回一個新的字符串,用newchar替換oldchar(替換字符)
String s = "asdfgasdfg"; String replace = s.replace("as", "bn"); System.out.println(replace);//bndfgbndfg
s.replace(target, replacement):返回一個新的字符串,replacement替換target
String s = "最好的隊長殷志源"; String replace = s.replace("最好的隊長", "丸子"); System.out.println(replace);//丸子殷志源
String s = "123"; int i = Integer.parseInt(s); System.out.println(i);//123
double d = 2.6; String s = String.valueOf(d); System.out.println(s);
String s = "abc"; char[] c = s.toCharArray(); System.out.println(Arrays.toString(c));//[a, b, c]
char[] c = new char[]{'a','b','c'}; String s = new String(c); System.out.println(s);//abc
String類型---->byte[]轉換:調用String的getBytes()方法
String s = "殷志源123"; byte[] b = s.getBytes(); System.out.println(Arrays.toString(b));//[-26, -82, -73, -27, -65, -105, -26, -70, -112, 49, 50, 51]
String s = "殷志源123"; byte[] b = s.getBytes("gbk"); System.out.println(Arrays.toString(b));//[-46, -13, -42, -66, -44, -76, 49, 50, 51]
編碼:字符串--->字節(能看的懂轉換爲看不懂的(計算機底層的二進制數據))
解碼:字節------->字符串(編碼的逆過程)
byte[]類型---->String類型轉換:調用String的構造器
在解碼時,要求解碼使用的解碼集必須與編碼時使用的字符集一致,不然會出現亂碼
String s = "殷志源123"; byte[] b1 = s.getBytes(); System.out.println(Arrays.toString(b1));//[-26, -82, -73, -27, -65, -105, -26, -70, -112, 49, 50, 51] String s1 = new String(b1); System.out.println(s1);//殷志源123