Java 語言的文本數據被保存為字符或字符串類型。字符及字符串的操作主要用到 String 類和 StringBuffer 類,如連接、修改、替換、比較和查找等。
定義字符串
直接定義字符串
直接定義字符串是指使用雙引號表示字符串中的內容,例如"Hello Java"、"Java 編程"等
String helloWorld ="Hello World";
使用 String 類定義
在 Java 中每個雙引號定義的字符串都是一個 String 類的對象。因此,可以通過使用 String 類的構造方法來創建字符串,該類位於 java.lang 包中。
String helloWorld =new String("Hello World");
字符串的連接
通過字符串連接,可以將兩個或多個字符串、字符、整數和浮點數等類型的數據連成一個更大的字符串。
使用連接運算符
“+”運算符是最簡單、最快捷,也是使用最多的字符串連接方式。在使用“+”運算符連接字符串和 int 型(或 double 型)數據時,“+”將 int(或 double)型數據自動轉換成 String 類型。
int i=10; String helloWorld="Hello World 第"+i+"次出現在文章中";
使用 concat() 方法
String 類的 concat() 方法實現了將一個字符串連接到另一個字符串的後面。
String hello = "Hello"; String world = "World"; String helloWorld = hello.concat(world);
獲取字符串長度
使用 String 類的 length() 方法
String hello = "Hello World"; System.out.println(hello.length());//輸出11
轉換大小寫
toLowerCase() 方法可以將字符串中的所有字符全部轉換成小寫,toUpperCase() 則將字符串中的所有字符全部轉換成大寫,非字母的字符不受影響。
String helloWorld="Hello World"; System.out.println(helloWorld.toLowerCase()); //輸出:helloworld System.out.println(helloWorld.toUpperCase()); //輸出:HELLOWORLD
去除空格
使用 String 類提供的 trim() 方法去掉首尾空格
String hello=" hello "; System.out.println(hello.trim());//去掉首尾空格後hello
字符串截取
String 類的 substring() 方法用於對字符串進行提取,該方法主要有兩種重載形式。
substring(int beginIndex)
從索引位置開始至結尾處的字符串部分。調用時,括號中是需要提取字符串的開始位置,方法的返回值是提取的字符串。
String helloWorld="Hello World"; String world=helloWorld.substring(6); System.out.println(world); //輸出:World
substring(int beginIndex,int endIndex)
beginIndex 表示截取的起始索引,截取的字符串中包括起始索引對應的字符;endIndex 表示結束索引,截取的字符串中不包括結束索引對應的字符,對於開始位置 beginIndex, Java 是基於字符串的首字符索引為 0 處理的,但是對於結束位置 endIndex,Java 是基於字符串的首字符索引為 1 來處理的
String helloWorld="Hello World"; System.out.println(helloWorld.substring(2,10));//輸出 llo Worl
分割字符串
String 類的 split() 方法可以按指定的分割符對目標字符串進行分割,分割後的內容存放在字符串數組中。str.split(String sign)與str.split(String sign,int limit),str 為需要分割的目標字符串;sign 為指定的分割符,可以是任意字符串;limit 表示分割後生成的字符串的限制個數,如果不指定,則表示不限制,直到將整個目標字符串完全分割為止。
String color="Red,Black,White,Yellow"; String[] arr1=color.split(",");//不限制元素個數 //arr1為 //Red //Black //White //Yellow String[] arr2=Colors.split(",",3); //限制元素個數為3 //arr2為 //Red //Black //White,Yellow
字符串的替換
String 類提供了 3 種字符串替換方法,分別是 replace()、replaceFirst() 和 replaceAll()
replace()
replace(String oldChar, String newChar) 方法用於將目標字符串中的指定字符(串)替換成新的字符(串)
String helloWorld="Hello Java"; System.out.println(words.replace("Java","World"));//輸出Hello World
replaceFirst()
replaceFirst(String regex, String replacement) 方法用於將目標字符串中匹配某正則表達式的第一個子字符串替換成新的字符串
String words="hello java,hello php"; System.out.println(words.replaceFirst("hello","你好 ")); //輸出:你好 java,hello php
replaceAll()
replaceAll(String regex, String replacement) 方法用於將目標字符串中匹配某正則表達式的所有子字符串替換成新的字符串
String words="hello java,hello php"; System.out.println(words.replaceAll("hello","你好 ")); //輸出:你好 java,你好 php
字符串的比較
比較字符串的常用方法有 3 個:equals() 方法、equalsIgnoreCase() 方法、 compareTo() 方法
equals()
equals() 方法將逐個地比較兩個字符串的每個字符是否相同。對於字符的大小寫,也在檢查的範圍之內。
String a="a"; String b="b"; System.out.println(a.equals(b)); //輸出 false
equalsIgnoreCase()
equalsIgnoreCase() 方法的作用和語法與 equals() 方法完全相同,唯一不同的是 equalsIgnoreCase() 比較時不區分大小寫
String a="ab"; String b="AB"; System.out.println(a.equals(b)); //輸出 true
compareTo()
compareTo() 方法用於按字典順序比較兩個字符串的大小,該比較是基於字符串各個字符的 Unicode 值。
String upperA="A"; String lowerA="a"; System.out.println(upperA.compareTo(lowerA)); //輸出為-32
查找字符串
字符串查找分為兩種形式:一種是在字符串中獲取匹配字符(串)的索引值,另一種是在字符串中獲取指定索引位置的字符。分別有三個方法indexOf()、lastlndexOf()和charAt()
indexOf()
indexOf() 方法用於返回字符(串)在指 定字符串中首次出現的索引位置,如果能找到,則返回索引值,否則返回 -1。
str.indexOf(value)
str.indexOf(value,int fromIndex)
str 表示指定字符串;value 表示待查找的字符(串);fromIndex 表示查找時的起始索引,如果不指定 fromIndex,則默認從指定字符串中的開始位置(即 fromIndex 默認為 0)開始查找。
String helloWorld="Hello Java"; int size=s.indexOf('v'); //size的結果為8
lastlndexOf()
lastIndexOf() 方法用於返回字符(串)在指定字符串中最後一次出現的索引位置,如果能找到則返回索引值,否則返回 -1。
str.lastIndexOf(value)
str.lastlndexOf(value, int fromIndex)
lastIndexOf() 方法的查找策略是從右往左查找,如果不指定起始索引,則默認從字符串的末尾開始查找。
String words="today,monday,Sunday"; System.out.println(words.lastIndexOf("day"));//輸出16
charAt()
charAt() 方法可以在字符串內根據指定的索引查找字符
String words="today,monday,sunday"; System.out.println(words.charAt(0)); //結果:t
StringBuffer類
除了通過 String 類創建和處理字符串之外,還可以使用 StringBuffer 類來處理字符串。StringBuffer 類可以比 String 類更高效地處理字符串。StringBuffer 類是可變字符串類,創建 StringBuffer 類的對象後可以隨意修改字符串的內容。每個 StringBuffer 類的對象都能夠存儲指定容量的字符串,如果字符串的長度超過了 StringBuffer 類對象的容量,則該對象的容量會自動擴大。
創建 StringBuffer
StringBuffer 類提供了 3 個構造方法來創建一個字符串
StringBuffer() 構造一個空的字符串緩衝區,並且初始化為 16 個字符的容量。
StringBuffer(int length) 創建一個空的字符串緩衝區,並且初始化為指定長度 length 的容量。
StringBuffer(String str) 創建一個字符串緩衝區,並將其內容初始化為指定的字符串內容 str,字符串緩衝區的初始容量為 16 加上字符串 str 的長度。
StringBuffer str1=new StringBuffer();//定義一個空的字符串緩衝區,含有16個字符的容量 StringBuffer str2=new StringBuffer(10);//定義一個含有10個字符容量的字符串緩衝區 StringBuffer str3=new StringBuffer("HelloWorld");//定義一個含有(16+4)的字符串緩衝區,"HelloWorld"為10個字符
追加字符串
StringBuffer 類的 append() 方法用於向原有 StringBuffer 對象中追加字符串。追加內容到當前 StringBuffer 對象的末尾,類似於字符串的連接。
StringBuffer buffer=new StringBuffer("hello,"); //創建一個 StringBuffer 對象 String str="World!"; buffer.append(str); //向 StringBuffer 對象追加 str 字符串 System.out.println(buffer.substring(0)); //輸出:Hello,World!
替換字符
StringBuffer 類的 setCharAt() 方法用於在字符串的指定索引位置替換一個字符。StringBuffer 對象.setCharAt(int index, char ch);
StringBuffer sb=new StringBuffer(“hello"); sb.setCharAt(1,'E'); System.out.println(sb); //輸出:hEllo
反轉字符串
StringBuffer 類中的 reverse() 方法用於將字符串序列用其反轉的形式取代。StringBuffer 對象.reverse();
StringBuffer sb=new StringBuffer("java"); sb.reverse(); System.out.println(sb); //輸出:avaj
刪除字符串
StringBuffer 類提供了 deleteCharAt() 和 delete() 兩個刪除字符串的方法
deleteCharAt()
deleteCharAt() 方法用於移除序列中指定位置的字符,StringBuffer 對象.deleteCharAt(int index);
StringBuffer sb=new StringBuffer("She"); sb.deleteCharAt(2); System.out.println(sb); //輸出:Se
delete()
delete() 方法用於移除序列中子字符串的字符,StringBuffer 對象.delete(int start,int end);start 表示要刪除字符的起始索引值(包括索引值所對應的字符),end 表示要刪除字符串的結束索引值(不包括索引值所對應的字符)。
StringBuffer sb=new StringBuffer("hello jack"); sb.delete(2,5); System.out.println(sb); //輸出:he jack