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

时间:2023-02-09 14:38:58

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

7 个解决方案

#1


17  

Use:

使用:

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

replaceAll takes a regular expression. This:

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

[,.]

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

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

#2


181  

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来读取文件,并将分隔符设置为您不希望成为单词的一部分的字符。

#3


7  

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(".","");

#4


4  

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中修改String。它们是不变的。您所能做的就是创建一个新字符串,它是旧字符串的子字符串,减去最后一个字符。

In some cases a StringBuffer might help you instead.

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

#5


3  

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

#6


0  

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

#7


0  

You can use replaceAll() method :

您可以使用replaceAll()方法:

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

etc..

等等..

#1


17  

Use:

使用:

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

replaceAll takes a regular expression. This:

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

[,.]

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

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

#2


181  

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来读取文件,并将分隔符设置为您不希望成为单词的一部分的字符。

#3


7  

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(".","");

#4


4  

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中修改String。它们是不变的。您所能做的就是创建一个新字符串,它是旧字符串的子字符串,减去最后一个字符。

In some cases a StringBuffer might help you instead.

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

#5


3  

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

#6


0  

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

#7


0  

You can use replaceAll() method :

您可以使用replaceAll()方法:

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

etc..

等等..