1 字串截取
1.1substring(int beginIndex)
提取從索引位置開始至結尾處的字串部分
1.2 substring(int beginIndex,int endIndex)
截取的字串中包括起始索引對應的字符;endIndex 表示結束索引,截取的字串中不包括結束索引對應的字符
public static void main(String[] args) {
// 下標從0開始
String str = "Java";
System.out.println("substring(2)結果:"+str.substring(2)); // 從下標為2的字串開始截到最后
System.out.println("substring(0,1)結果:"+str.substring(0,1));
System.out.println("substring(0,4)結果:"+str.substring(1,4));
}
substring(2)結果:va
substring(0,1)結果:J
substring(0,4)結果:ava
2 分割字串
2.1 str.split(String sign)
2.2 str.split(String sign,int limit)
str 為需要分割的目標字串,
sign 為指定的分割符,可以是任意字串,
limit 表示分割后生成的字串的限制個數,如果不指定,則表示不限制,直到將整個目標字串完全分割為止
public static void main(String[] args) {
String family = "Grandpa,grandma,Dad,Mum,Baby";
String[] arr1 = family.split(","); // 不限制元素個數
String[] arr2 = family.split(",", 3); // 限制元素個數為3
System.out.println("所有家庭成員為:");
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
System.out.println("前三組家庭成員為:");
for (int j = 0; j < arr2.length; j++) {
System.out.println(arr2[j]);
}
}
所有家庭成員為:
Grandpa
grandma
Dad
Mum
Baby
前三組家庭成員為:
Grandpa
grandma
Dad,Mum,Baby
3 字串比較(4種方法)
3.1 equals()
equals() 方法將逐個地比較兩個字串的每個字符是否相同
3.2 equalsIgnoreCase()
equalsIgnoreCase() 方法的作用和語法與 equals() 方法完全相同,唯一不同的是 equalsIgnoreCase() 比較時不區分大小寫
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
String str3 = "ABC";
System.out.println(str1.equals(str2)); // 輸出 true
System.out.println(str1.equals(str3)); // 輸出 false
System.out.println(str1.equalsIgnoreCase(str3)); // 輸出 true
}
3.3 equals()與==的比較
equals() 方法比較字串物件中的字符,而==運算子比較兩個物件參考看它們是否參考相同的實體
public static void main(String[] args) {
String s1 = "java";
String s2 = new String(s1);
System.out.println(s1.equals(s2)); // 輸出true
System.out.println(s1 == s2); // 輸出false
}
3.4 compareTo() 方法
compareTo() 方法用于按字典順序比較兩個字串的大小,該比較是基于字串各個字符的 Unicode 值
public static void main(String[] args) {
String str = "J";
String str1 = "j";
System.out.println("str.compareTo(str1)的結果是:" + str.compareTo(str1));
System.out.println("str1.compareTo(str)的結果是:" + str1.compareTo(str));
System.out.println("str1.compareTo('a')的結果是:" + str1.compareTo("j"));
}
str.compareTo(str1)的結果是:-32
str1.compareTo(str)的結果是:32
str1.compareTo('a')的結果是:0
4 String字串和整型int的相互轉換
4.1 String 字串轉整型 int
Integer.parseInt(str)
Integer.valueOf(str).intValue()
public static void main(String[] args) {
String str = "123";
System.out.println("Integer.parseInt(str) : " + Integer.parseInt(str));
System.out.println("Integer.valueOf(str).intValue() : " + Integer.valueOf(str).intValue());
}
Integer.parseInt(str) : 123
Integer.valueOf(str).intValue() : 123
4.2 int轉換為String
String s = String.valueOf(i);
String s = Integer.toString(i);
String s = “” + i;
public static void main(String[] args) {
int num = 10;
System.out.println("String.valueOf(num):" + String.valueOf(num));
System.out.println("Integer.toString:" + Integer.toString(num));
String str3 = num + "";
System.out.println("str3:" + str3);
}
String.valueOf(num):10
Integer.toString:10
str3:10
5 去除字串中的空格
public static void main(String[] args) {
String str = " hello ";
System.out.println(str.length()); // 輸出 7
System.out.println(str.trim().length()); // 輸出 5
}
6 字串大小寫轉換
6.1 toLowerCase()
將字串中的字母全部轉換為小寫,非字母不受影響
6.2 toUpperCase()
將字串中的字母全部轉換為大寫,非字母不受影響
public static void main(String[] args) {
String en = "Snow White";
System.out.println("原字串:"+en);
System.out.println("使用 toLowerCase() 方法之后為:"+en.toLowerCase());
System.out.println("使用 toUpperCase() 方法之后為:"+en.toUpperCase());
}
原字串:Snow White
使用 toLowerCase() 方法之后為:snow white
使用 toUpperCase() 方法之后為:SNOW WHITE
7 字串替換
7.1 replace() 方法
replace() 方法用于將目標字串中的指定字符(串)替換成新的字符(串)
public static void main(String[] args) {
String words = "hello java";
System.out.println("原始字串是'"+words+"'");
System.out.println("replace(\"l\",\"D\")結果:"+words.replace("l","D"));
System.out.println("replace(\"hello\",\"你好\")結果:"+words.replace("hello","你好 "));
}
原始字串是'hello java'
replace("l","D")結果:heDDo java
replace("hello","你好")結果:你好 java
7.2 replaceFirst()
將目標字串中匹配某正則運算式的第一個子字串替換成新的字串
public static void main(String[] args) {
String words = "hello Dad,hello Mum";
String newStr = words.replaceFirst("hello","你好 ");
System.out.println(newStr); // 輸出:你好 Dad,hello Mum
}
7.3 replaceAll()
public static void main(String[] args) {
String words = "hello Dad,hello Mum";
String newStr = words.replaceAll("hello","你好 ");
System.out.println(newStr); // 輸出:你好 Dad,你好 Mum
}
8 查找字串
8.1 indexOf() 方法
indexOf() 方法用于回傳字符(串)在指定字串中首次出現的索引位置,如果能找到,則回傳索引值,否則回傳 -1
public static void main(String[] args) {
String s = "Hello Java";
System.out.println(s.indexOf('v')); // 輸出結果為8
System.out.println(s.indexOf('m')); // 找不到字母m的位置,輸出結果為-1
}
8.2 lastlndexOf() 方法
lastIndexOf() 方法用于回傳字符(串)在指定字串中最后一次出現的索引位置,如果能找到則回傳索引值,否則回傳 -1
public static void main(String[] args) {
String words="Friday,Sunday";
System.out.println("原始字串是'"+words+"'");
System.out.println("lastIndexOf(\"day\")結果:"+words.lastIndexOf("day")); // lastIndexOf("day")結果:10
}
9 StringBuffer 類
9.1 創建StringBuffer
StringBuffer() 構造一個空的字串緩沖區,并且初始化為 16 個字符的容量,
StringBuffer(int length) 創建一個空的字串緩沖區,并且初始化為指定長度 length 的容量,
StringBuffer(String str) 創建一個字串緩沖區,并將其內容初始化為指定的字串內容 str,字串緩沖區的初始容量為 16 加上字串 str 的長度,
public static void main(String[] args) {
StringBuffer str1 = new StringBuffer();
StringBuffer str2 = new StringBuffer(5);
StringBuffer str3 = new StringBuffer("我愛java");
System.out.println(str1.capacity()); // 輸出 16
System.out.println(str2.capacity()); // 輸出 5
System.out.println(str3.capacity()); // 輸出 22
}
9.2 追加字串 append()
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("hello,");
String str = "World!";
buffer.append(str);
System.out.println(buffer); // 輸出:Hello,World!
}
9.3 替換字符 setCharAt()
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("hello");
sb.setCharAt(1,'E');
System.out.println(sb); // 輸出:hEllo
}
9.4 洗掉字串
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("She");
sb.deleteCharAt(2);
System.out.println(sb); // 輸出:Sh
}
10 String、StringBuffer和StringBuilder類的區別
執行緒安全:
StringBuffer:執行緒安全
StringBuilder:執行緒不安全
速度:
一般情況下,速度從快到慢為 StringBuilder > StringBuffer > String,當然這是相對的,不是絕對的,
使用環境:
操作少量的資料使用 String,
單執行緒操作大量資料使用 StringBuilder,
多執行緒操作大量資料使用 StringBuffer,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281608.html
標籤:java
上一篇:關于單例模式,你應該了解這些
