简体   繁体   English

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

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

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方法来做到这一点,但我无法解决它。

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 StringTokenizer to read the file and set the delimiters to be the characters you don't want to be part of words. 另外一种解决问题的方法是:您可能还需要考虑使用StringTokenizer来读取文件并将分隔符设置为您不希望成为单词的一部分的字符。

Use: 使用:

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

replaceAll takes a regular expression . replaceAll采用正则表达式 This: 这个:

[,.]

...looks for each comma and/or period. ...查找每个逗号和/或句号。

To remove the last character do as Mark Byers said 要删除最后一个角色,请按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(".","");

You can't modify a String in Java. 您无法在Java中修改String。 They are immutable. 他们是不变的。 All you can do is create a new string that is substring of the old string, minus the last character. 您所能做的就是创建一个新字符串,它是旧字符串的子字符串,减去最后一个字符。

In some cases a StringBuffer might help you instead. 在某些情况下,StringBuffer可能会帮助您。

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替换\\“space”,它就不能正常工作

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

or 要么

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

Note that the word boundaries also depend on the Locale. 请注意,单词边界也取决于区域设置。 I think the best way to do it using standard java.text.BreakIterator. 我认为使用标准java.text.BreakIterator进行此操作的最佳方法。 Here is an example from the java.sun.com tutorial. 以下是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

You can use replaceAll() method : 您可以使用replaceAll()方法:

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

etc.. 等等..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM