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

后端 未结 7 1363
醉梦人生
醉梦人生 2021-02-05 00:45

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 (.)

相关标签:
7条回答
  • 2021-02-05 01:01

    Use:

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

    replaceAll takes a regular expression. This:

    [,.]
    

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

    0 讨论(0)
  • 2021-02-05 01:03

    To remove the last character do as Mark Byers said

    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.

    as in:

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

    and

    s = s.replace(".","");
    
    0 讨论(0)
  • 2021-02-05 01:14

    You can use replaceAll() method :

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

    etc..

    0 讨论(0)
  • 2021-02-05 01:15

    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.

    0 讨论(0)
  • 2021-02-05 01:16

    The best method is what Mark Byers explains:

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

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

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

    or

    String.replaceAll("\\$", "");   //if it is a path
    
    0 讨论(0)
  • 2021-02-05 01:20

    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.

    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

    0 讨论(0)
提交回复
热议问题