介紹
在本文中,您將學習幾種不同的方法來從 Java 中的 String
物件中刪除字符。儘管 String
類沒有 remove()
方法,但您可以使用 replace()
方法的變化形式和 substring()
方法來從字符串中刪除字符。
注意: String
物件是不可變的,這意味著它們在創建後無法更改。本文中描述的所有 String
類方法都會返回一個新的 String
物件,並且不會更改原始物件。您使用的字符串類型取決於程序的要求。了解更多關於 其他類型的字符串類 和 Java 中字符串為什麼是不可變的。
String
類具有以下方法,您可以使用這些方法來替換或刪除字符:
replace(char oldChar, char newChar)
:返回一個新的String
物件,其中替換了給定字符串中所有oldChar
的出現項目為newChar
。您還可以使用replace()
方法,格式為replace(CharSequence target, CharSequence replacement)
,以返回一個新的String
物件,該物件將替換給定字符串中的子字符串。replaceFirst(String regex, String replacement)
: 返回一個新的String
對象,該對象用替換字符串替換給定字符串中匹配正則表達式的第一個子字符串。replaceAll(String regex, String replacement)
: 返回一個新的String
對象,該對象用替換字符串替換給定字符串中匹配正則表達式的每個子字符串。substring(int start, int end)
: 返回一個新的String
對象,該對象包含當前序列中當前包含的字符子序列。子字符串從指定的開始位置開始,延伸到索引結束減1的字符。
請注意,replaceAll()
和replaceFirst()
方法的第一個參數是一個正則表達式。您可以使用正則表達式從字符串中刪除模式。
注意: 當您使用replace()
方法時,需要使用雙引號來指示文字字符串值。如果使用單引號,則JRE會假定您正在指示字符常量,編譯程序編譯時將出錯。
從Java中的字符串中刪除字符
您可以使用replace()
方法在Java中從字符串中刪除所有特定字符,將該字符替換為空字符串。以下示例代碼從給定字符串中刪除所有小寫字母“a
”的出現:
String str = "abc ABC 123 abc";
String strNew = str.replace("a", "");
Outputbc ABC 123 bc
在Java中從字符串中刪除空格
您可以使用replace()
方法在Java中從字符串中刪除空格,將空格替換為空字符串。以下示例代碼從給定字符串中刪除所有空格:
String str = "abc ABC 123 abc";
String strNew = str.replace(" ", "");
OutputabcABC123abc
在Java中從字符串中刪除子字符串
您可以使用replaceFirst()
方法在Java中僅刪除字符串中字符或子字符串的第一次出現,將該字符或子字符串替換為空字符串。以下示例代碼從給定字符串中刪除第一次出現的“ab
”:
String str = "abc ABC 123 abc";
String strNew = str.replaceFirst("ab", "");
Outputc ABC 123 abc
在Java中從字符串中刪除所有小寫字母
您可以使用正則表達式通過使用replace.All()
方法將與給定模式匹配的字符從字符串中刪除。以下示例代碼從給定字符串中刪除所有小寫字母:
String str = "abc ABC 123 abc";
String strNew = str.replaceAll("([a-z])", "");
OutputABC 123
在Java中從字符串中刪除最後一個字符
沒有特定的方法來替換或刪除字符串的最後一個字符,但您可以使用String substring()方法截斷字符串。以下示例代碼從給定字符串中刪除最後一個字符:
String str = "abc ABC 123 abc";
String strNew = str.substring(0, str.length()-1);
Outputabc ABC 123 ab
試試吧
下面的示例文件定義了一個包含本文提供的所有方法示例的類,並在調用每個方法後打印出結果。您可以使用此示例代碼在不同的字符串上自行嘗試不同的匹配模式和替換值。
如果您已安裝了Java,可以創建一個名為JavaStringRemove.java
的新文件,並將以下代碼添加到文件中:
public class JavaStringRemove {
public static void main(String[] args) {
String str = "abc ABC 123 abc";
// 從Java字符串中刪除字符
System.out.println("String after removing all the 'a's = "+str.replace("a", ""));
// 從Java字符串中刪除空格
System.out.println("String after removing all the spaces = "+str.replace(" ", ""));
// 從Java字符串中刪除子字符串
System.out.println("String after removing the first 'ab' substring = "+str.replaceFirst("ab", ""));
// 從Java字符串中刪除所有小寫字母
System.out.println("String after removing all the lowercase letters = "+str.replaceAll("([a-z])", ""));
// 從Java字符串中刪除最後一個字符
System.out.println("String after removing the last character = "+str.substring(0, str.length()-1));
}
}
編譯並運行該程序:
- javac JavaStringRemove.java
- java JavaStringRemove
您將獲得以下輸出:
OutputString after removing all the 'a's = bc ABC 123 bc
String after removing all the spaces = abcABC123abc
String after removing the first 'ab' substring = c ABC 123 abc
String after removing all the lowercase letters = ABC 123
String after removing the last character = abc ABC 123 ab
JavaStringRemove
示例類中的每個方法都在給定的字符串上操作。輸出顯示了每個方法中指定的字符已從字符串中刪除。
結論
在這篇文章中,您學到了使用 Java 中的 String
類別的方法來從字串中刪除字符的各種方式,包括 replace()
、replaceAll()
、replaceFirst()
和 substring()
。繼續您的學習,了解更多 Java 教程。
Source:
https://www.digitalocean.com/community/tutorials/java-remove-character-string