如何在Java中删除字符串中的一个字符

简介

在这篇文章中,您将学习几种不同的方式来从Java的String对象中删除字符。虽然String类没有remove()方法,但是您可以使用replace()方法的变体和substring()方法来删除字符串中的字符。

Note

注意:字符串对象是不可变的,这意味着它们在创建后不能更改。本文介绍的所有String类方法都会返回一个新的String对象,而不会改变原始对象。您使用的字符串类型取决于程序的要求。了解有关其他类型的字符串类以及Java中字符串为什么是不可变的的更多信息。

String类具有以下方法,您可以使用这些方法来替换或删除字符:

  • replace(char oldChar, char newChar): Returns a new String object that replaces all of the occurrences of oldChar in the given string with newChar. You can also use the replace() method, in the format replace(CharSequence target, CharSequence replacement), to return a new String object that replaces a substring in the given string.
  • replaceFirst(String regex, String replacement): Returns a new String object that replaces the first substring that matches the regular expression in the given string with the replacement.
  • replaceAll(String regex, String replacement): Returns a new String object that replaces each substring that matches the regular expression in the given string with the replacement.
  • substring(int start, int end): Returns a new String object that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end minus 1.

注意replaceAll()和replaceFirst()方法的第一个参数是一个正则表达式。您可以使用正则表达式从字符串中删除一个模式。

Note

注意:在使用replace()方法时,必须使用双引号来表示字面字符串值。如果你使用单引号,那么Java运行时环境会认为你正在指定一个字符常量,当你编译程序时会产生错误。

在Java中从字符串中删除一个字符

可以使用Java中的replace()方法将字符串中所有的字符实例替换为空字符串,从而从字符串中删除该字符。以下示例代码将删除给定字符串中所有小写字母”a”的出现次数。

String str = "abc ABC 123 abc";
String strNew = str.replace("a", "");
Output

bc ABC 123 bc

在Java中删除字符串中的空格

您可以使用Java中的replace()方法将字符串中的空格替换为空字符串来去除字符串中的空格。以下示例代码从给定的字符串中移除所有空格。

String str = "abc ABC 123 abc";
String strNew = str.replace(" ", "");
Output

abcABC123abc

在Java中从字符串中删除子字符串

你可以使用Java中的replaceFirst()方法来删除字符串中字符或子字符串的第一次出现,将字符或子字符串替换为空字符串。以下示例代码从给定的字符串中删除了”ab”的第一次出现。

String str = "abc ABC 123 abc";
String strNew = str.replaceFirst("ab", "");
Output

c ABC 123 abc

从Java字符串中删除所有小写字母。

你可以使用正则表达式来删除符合给定模式的字符。在Java中,可以使用replace.All()方法将这些字符替换为空字符串。下面的示例代码从给定的字符串中删除所有小写字母。

String str = "abc ABC 123 abc";
String strNew = str.replaceAll("([a-z])", "");
Output

ABC 123

在Java中删除字符串的最后一个字符

没有特定的方法来替换或删除字符串的最后一个字符,但是你可以使用String substring()方法来截取字符串。以下示例代码从给定的字符串中删除最后一个字符。

String str = "abc ABC 123 abc";
String strNew = str.substring(0, str.length()-1);
Output

abc ABC 123 ab

试试看

下面的示例文件定义了一个包含本文提供的所有方法示例的类,并在调用给定字符串上的每个方法后打印出结果。您可以使用这个示例代码在不同的字符串上尝试不同的匹配模式和替换值。

如果你已经安装了Java,你可以创建一个名为JavaStringRemove.java的新文件,并将以下代码添加到文件中:

Java字符串删除.java

public class JavaStringRemove {

	public static void main(String[] args) {
    	String str = "abc ABC 123 abc";

		// Remove a character from a string in Java
		System.out.println("String after removing all the 'a's = "+str.replace("a", ""));

		// Remove spaces from a string in Java
		System.out.println("String after removing all the spaces = "+str.replace(" ", ""));

		// Remove a substring from a string in Java	
		System.out.println("String after removing the first 'ab' substring = "+str.replaceFirst("ab", ""));

		// Remove all the lowercase letters from a string in Java
		System.out.println("String after removing all the lowercase letters = "+str.replaceAll("([a-z])", ""));

		// Remove the last character from a string in Java
		System.out.println("String after removing the last character = "+str.substring(0, str.length()-1));
	}

}

编译并运行程序。

  1. javac JavaStringRemove.java
  2. java JavaStringRemove

 

你获得了以下的输出结果。

Output

String 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示例类中,每个方法都对给定的字符串进行操作。输出显示每个方法中指定的字符已从字符串中删除。

结论

在这篇文章中,你学到了使用String类的方法在Java中删除字符串中的字符的各种方法,包括replace()、replaceAll()、replaceFirst()和substring()。继续学习更多的Java教程。

广告
将在 10 秒后关闭
bannerAds