Java語言中字符串的基本操作:
1.獲取字符串的長度
獲取字符串的長度可以使用String類的length()方法,其定義如下:
public int length()
此方法返回字符創的字符個數,示例:
String s = "Hello World";
2.轉換字符串的大小寫
public String toLowerCase() //把字符串內容轉換為小寫
public String toUpperCase() //把字符串內容轉換為大寫
示例:
String s = "Hello World";
System.out.println(s.toLowerCase()); //輸出:hello world
System.out.println(s.toUpperCase()); //輸出:HELLO WORLD
3.去掉字符串的首尾空格
public String trim()
示例:
String s1 = " Hello World ";
String s2 = "Hello World";
System.out.println(s1.equals(s2)); //輸出false
System.out.println(s1.trim().equals(s2)); //輸出true
4.比較字符串的大小
4.1使用equals()方法比較字符串
public boolean equals(Object str)
示例:
String s1 = "Hello World";
String s2 = "Hello World";
String s3 = "Hello world";
System.out.println(s1.equals(s2)); //輸出true
System.out.println(s2.equals(s3)); //輸出false
4.2使用equalsIgnoreCase()方法比較字符串
public boolean equalsIgnoreCase() //該方法忽略大小寫
示例:
String s1 = "Hello World";
String s2 = "hello world";
System.out.println(s1.equalsIgnoreCase(s2)); //輸出true
5.字符串查找
5.1在字符串中獲取匹配字符(串)的索引值
5.1.1 indexOf()方法
indexOf()方法有四種重載形式,如下:
public int indexOf(int ch) //返回在指定字符的第一個發生的字符串中的索引。
public int indexOf(int ch, int fromIndex) //返回在指定字符的第一個發生的字符串中的索引,在指定的索引處開始搜索。
public int indexOf(String str) //返回此字符串的指定子字符串中第一個出現的索引
public int indexOf(String str, int fromIndex) //返回此字符串的指定子字符串中第一個出現在索引中,從指定索引處開始。
5.1.2 lastIndexOf方法
lastIndexOf()與indexOf()類似,不同之處在於lastIndexOf()是從后往前搜索lastIndexOf()方法也有四種重載形式,如下:
public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
5.1.3 獲取指定索引位置的字符
public char charAt(int index)
示例:
String s = "Hello World";
char ch = s.charAt(7);
System.out.println(ch); //輸出o
6.字符串分割
String類提供的split()方法,可以按指定的分隔符對目標字符串進行分割,分割后的內容存放在字符串數組中,該方法主要有如下兩種重載形式:
public String [] split(String regex, int limit)
public String [] split(String regex)
其中,regex為指定的分割符,可以是任意字符串,也可以是正則表達式;limit表示分割后生成的字符串的限制個數,如果不指定,則表示不限制,直到將整個目標字符串完全分割為止:
示例:
String s = "H;e;l;l;o;w;o;r;l;d";
String [] array1 = s.split(";");
String [] array2 = s.split(";",6);
System.out.println("array1數組中的元素如下:");
for(int i = 0; i
System.out.print(array1[i]+"\t");
}
System.out.println("\narray2數組中的元素如下:");
for(int i = 0; i
System.out.print(array2[i]+"\t");
}
上述代碼的執行結果是:
array1數組中的元素如下:
H e l l o w o r l d
array2數組中的元素如下:
H e l l o w;o;r;l;d
7.字符串截取
String類的subString()方法用於對字符串進行截取,該方法主要有如下兩種重載形式:
public String subString(int beginIndex)
public String subString(int beginIndex, int endIndex)
示例:
String s = "Hello World";
String s1 = s.subString(6); //返回索引為6到末尾的字符串
String s2 = s.subString(2,5); //返回索引為2~5之間的字符組成的字符串,包括索引為2的字符
System.out.println(s1); //輸出World
System.out.println(s2); //輸出llo
8.字符串替換
8.1 使用replace()方法
public String replace(char oldChar, char newChar) //replace()方法會將所有oldChar替換成newChar
示例:
String s = "Hello World";
String news = s.replace("World","Java");
System.out.println(news); //輸出:Hello Java
8.2使用replaceFirst()方法
public String replaceFirst(String regex, String replacement)
//replacement()方法用於將目標字符串匹配指定的正則表達式的第一個子字符串替換為新的字符串。
示例:
String s = "Hello World Hello World";
String news = s.replaceFirst("World","Java");
System.out.println(news); //輸出Hello Java Hello World
8.3使用replaceAll()方法
public String replaceAll(String regex, String replacement)
//replacement()方法用於將目標字符串匹配指定的正則表達式的所有子字符串替換為新的字符串。
示例:
String s = "Hello World Hello World";
String news = s.replaceFirst("World","Java");
System.out.println(news); //輸出Hello Java Hello Java
9.字符串連接
在Java中,字符串的連接方式有兩種:一種是使用“+”運算符來對多個該字符串進行連接,另一種是使用String類的concat()方法。先介紹使用concat()方法進行連接。
public String concat(String str)
//其中,參數str表示連接到字符串后面的字符串。注:String類的concat()方法一次只能連接兩個字符串,如果需要連接多個字符串,需要多次調用concat()方法。
示例:
String str = "Hello!";
String name = "My name is Michael Scofield!";
System.out.println(str.concat(name)); //輸出:Hello!My name is Michael Scofield!