java字符串删除字符串_Java从字符串中删除字符

java字符串删除字符串

Sometimes we have to remove character from String in java program. But java String class doesn’t have remove() method. So how would you achieve this?

有时我们必须在Java程序中从String中删除字符。 但是java String类没有remove()方法。 那么您将如何实现呢?

Java从字符串中删除字符 (Java Remove Character from String)

java remove character from string

If you notice String class, we have replace() methods with different variations. Let’s see what all overloaded replace() methods String class has;


如果您注意到String类,我们有具有不同变体的replace()方法。 让我们看看所有重载的replace()方法都有什么?

  1. replace(char oldChar, char newChar): Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

    replace(char oldChar, char newChar) :返回一个字符串,该字符串是用newChar替换此字符串中所有出现的oldChar的结果。
  2. replace(CharSequence target, CharSequence replacement): Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

    replace(CharSequence target, CharSequence replacement) :使用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个子字符串。
  3. replaceFirst(String regex, String replacement): Replaces the first substring of this string that matches the given regular expression with the given replacement.

    replaceFirst(String regex, String replacement) :使用给定的替换项替换与给定的正则表达式匹配的此字符串的第一个子字符串。
  4. replaceAll(String regex, String replacement): Replaces each substring of this string that matches the given regular expression with the given replacement.

    replaceAll(String regex, String replacement) :使用给定的替换项替换与给定的正则表达式匹配的此字符串的每个子字符串。

So can we use replace('x','');? If you will try this, you will get compiler error as Invalid character constant. So we will have to use other replace methods that take string, because we can specify “” as empty string to be replaced.

那么我们可以使用replace('x',''); ? 如果尝试这种方法,则会收到编译器错误,即Invalid character constant 。 因此,我们将不得不使用其他采用字符串的替换方法,因为我们可以将“”指定为要替换的空字符串。

Java字符串删除字符示例 (Java String Remove Character Example)

Below code snippet shows how to remove all occurrences of a character from the given string.

下面的代码段显示了如何从给定的字符串中删除所有出现的字符。

String str = "abcdDCBA123";
String strNew = str.replace("a", ""); // strNew is 'bcdDCBA123'

Java从字符串中删除子字符串 (Java Remove substring from String)

Let’s see how to remove first occurrence of “ab” from the String.

让我们看看如何从字符串中删除首次出现的“ ab”。

String str = "abcdDCBA123";
String strNew = str.replaceFirst("ab", ""); // strNew is 'cdDCBA123'

Notice that replaceAll and replaceFirst methods first argument is a regular expression, we can use it to remove a pattern from string. Below code snippet will remove all small case letters from the string.

注意, replaceAllreplaceFirst方法的第一个参数是一个正则表达式 ,我们可以使用它从字符串中删除模式。 下面的代码段将从字符串中删除所有小写字母。

String str = "abcdDCBA123";
String strNew = str.replaceAll("([a-z])", ""); // strNew is 'DCBA123'

Java从字符串中删除空格 (Java Remove Spaces from String)

String str = "Hello World Java Users";
String strNew = str.replace(" ", ""); //strNew is 'HelloWorldJavaUsers'

Java从字符串中删除最后一个字符 (Java Remove Last Character from String)

There is no method to replace or remove last character from string, but we can do it using string substring method.

没有替换或删除字符串中最后一个字符的方法,但是我们可以使用字符串子字符串方法来实现。

String str = "Hello World!";
String strNew = str.substring(0, str.length()-1); //strNew is 'Hello World'

Java字符串删除字符和字符串示例 (Java String Remove Character and String Example)

Here is the complete java class for the examples shown above.

这是上面显示的示例的完整java类。

package com.journaldev.examples;

public class JavaStringRemove {

	public static void main(String[] args) {
		String str = "abcdDCBA123";

		System.out.println("String after Removing 'a' = "+str.replace("a", ""));
		
		System.out.println("String after Removing First 'a' = "+str.replaceFirst("ab", ""));

		System.out.println("String after replacing all small letters = "+str.replaceAll("([a-z])", ""));
	}

}

Output produced by above program is:

上面程序产生的输出是:

String after Removing 'a' = bcdDCBA123
String after Removing First 'a' = cdDCBA123
String after replacing all small letters = DCBA123

That’s all for removing character or substring from string in java program.

这就是从Java程序中的字符串中删除字符或子字符串的全部。

翻译自: https://www.journaldev.com/18361/java-remove-character-string

java字符串删除字符串

  • 9
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值