Removing certain characters from a string

前端 未结 8 1424
清酒与你
清酒与你 2020-12-05 10:46

I am thinking about using String.replaceAll() to remove certain characters in my string. It is unclear which characters are going to be removed (i.e. which char

相关标签:
8条回答
  • 2020-12-05 11:37

    If you're already using the library, Guava makes this easy with CharMatcher

    String charsToRemove = "%^#";
    String stringToFilter = "I have 20% of my assets in #2 pencils! :^)";
    
    String filtered = CharMatcher.anyOf(charsToRemove).removeFrom(stringToFilter);
    
    0 讨论(0)
  • 2020-12-05 11:38

    I guess, the below code will help you.

        String input = "Just to clarify, I will have strings of varying "
          + "lengths. I want to strip characters from it, the exact "
          + "ones to be determined at runtime, and return the "
          + "resulting string.";
        String regx = ",.";
        char[] ca = regx.toCharArray();
        for (char c : ca) {
            input = input.replace(""+c, "");
        }
        System.out.println(input);
    
    0 讨论(0)
提交回复
热议问题