如何从 Java 中的特定字符串中删除特定字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1962181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:50:18  来源:igfitidea点击:

How do I delete specific characters from a particular String in Java?

javastringimmutability

提问by Slavisa Perisic

For example I'm extracting a text String from a text file and I need those words to form an array. However, when I do all that some words end with comma (,) or a full stop (.) or even have brackets attached to them (which is all perfectly normal).

例如,我从文本文件中提取一个文本字符串,我需要这些词来形成一个数组。但是,当我完成所有这些操作时,有些单词以逗号 (,) 或句号 (.) 结尾,甚至连括号(这都是完全正常的)。

What I want to do is to get rid of those characters. I've been trying to do that using those predefined String methods in Java but I just can't get around it.

我想做的是摆脱这些角色。我一直在尝试使用 Java 中的那些预定义 String 方法来做到这一点,但我无法绕过它。

采纳答案by OMG Ponies

Use:

用:

String str = "whatever";
str = str.replaceAll("[,.]", "");

replaceAlltakes a regular expression. This:

replaceAll采用正则表达式。这个:

[,.]

...looks for each comma and/or period.

...查找每个逗号和/或句点。

回答by bmargulies

You can't modify a String in Java. They are immutable. All you can do is create a new string that is substring of the old string, minus the last character.

您不能在 Java 中修改字符串。它们是不可变的。您所能做的就是创建一个新字符串,该字符串是旧字符串的子字符串,减去最后一个字符。

In some cases a StringBuffer might help you instead.

在某些情况下,StringBuffer 可能会帮助您。

回答by Mark Byers

Reassign the variable to a substring:

将变量重新分配给子字符串:

s = s.substring(0, s.length() - 1)

Also an alternative way of solving your problem: you might also want to consider using a StringTokenizerto read the file and set the delimiters to be the characters you don't want to be part of words.

还有一种解决问题的替代方法:您可能还需要考虑使用StringTokenizer读取文件并将分隔符设置为您不想成为单词一部分的字符。

回答by Chandra Patni

Note that the word boundaries also depend on the Locale. I think the best way to do it using standard java.text.BreakIterator. Here is an example from the java.sun.com tutorial.

请注意,单词边界也取决于语言环境。我认为最好的方法是使用标准的 java.text.BreakIterator。这是 java.sun.com 教程中的一个示例。

import java.text.BreakIterator;
import java.util.Locale;

public static void main(String[] args) {
    String text = "\n" +
            "\n" +
            "For example I'm extracting a text String from a text file and I need those words to form an array. However, when I do all that some words end with comma (,) or a full stop (.) or even have brackets attached to them (which is all perfectly normal).\n" +
            "\n" +
            "What I want to do is to get rid of those characters. I've been trying to do that using those predefined String methods in Java but I just can't get around it.\n" +
            "\n" +
            "Every help appreciated. Thanx";
    BreakIterator wordIterator = BreakIterator.getWordInstance(Locale.getDefault());
    extractWords(text, wordIterator);
}

static void extractWords(String target, BreakIterator wordIterator) {
    wordIterator.setText(target);
    int start = wordIterator.first();
    int end = wordIterator.next();

    while (end != BreakIterator.DONE) {
        String word = target.substring(start, end);
        if (Character.isLetterOrDigit(word.charAt(0))) {
            System.out.println(word);
        }
        start = end;
        end = wordIterator.next();
    }
}

Source: http://java.sun.com/docs/books/tutorial/i18n/text/word.html

来源:http: //java.sun.com/docs/books/tutorial/i18n/text/word.html

回答by fastcodejava

You can use replaceAll()method :

您可以使用replaceAll()方法:

String.replaceAll(",", "");
String.replaceAll("\.", "");
String.replaceAll("\(", "");

etc..

等等..

回答by Tom Neyland

To remove the last character do as Mark Byerssaid

要删除最后一个字符,请按照Mark Byers所说

s = s.substring(0, s.length() - 1);

Additionally, another way to remove the characters you don't want would be to use the .replace(oldCharacter, newCharacter)method.

此外,另一种删除不需要的字符的.replace(oldCharacter, newCharacter)方法是使用该方法。

as in:

如:

s = s.replace(",","");

and

s = s.replace(".","");

回答by AlexGach

The best method is what Mark Byers explains:

最好的方法是 Mark Byers 解释的:

s = s.substring(0, s.length() - 1)

For example, if we want to replace \ to space " " with ReplaceAll, it doesn't work fine

例如,如果我们想用 ReplaceAll 将 \ 替换为空格“”,则无法正常工作

String.replaceAll("\", "");

or

或者

String.replaceAll("\$", "");   //if it is a path