Remove all characters from the string except letters and numbers in Java


Help! I can't remove all characters from the string except letters and numbers. In the beginning, I have a static method for deleting a character and then the code is not working.
Thank you

public static String removeChar(String s, char c) {
       String r = "";
       for (int i = 0; i < s.length(); i ++) {
          if (s.charAt(i) != c) r += s.charAt(i);
       }
       return r;
}

public static void main(String[] args) {

    int n = 0;
    Scanner sc = new Scanner(System.in);
    System.out.print("Введите свой текст: ");
    String self = sc.nextLine();

    for (char ch : self.toCharArray()) {
        if ( !Character.isLetterOrDigit(ch) ) {
            n++;
            removeChar(self, ch);
        }
    }

    System.out.println("Количество других символов: " + n);
    System.out.println( self );

}
Author: Wiktor Stribiżew, 2012-04-22

8 answers

For such tasks, it is better to use regular expressions. The code will be much simpler and clearer.

String self = getString();//как-нибудь получаем строку
String result = self.replaceAll("\\w|\\d", "")//регулярным выражением заменяем буквы(\\w) и цифры(\\d) на пустую строку, то есть удаляем.
 4
Author: Синицын Артём, 2012-04-22 11:54:13

@Luchnik, you were not on the right track.

public class HelloWorld {
    public static void main (String... av) {

        String s = "  --abc  678 .. Z";

        System.out.println("End "+delNoDigOrLet(s));
    }

    private static String delNoDigOrLet (String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (Character .isLetterOrDigit(s.charAt(i)))
                sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}
 7
Author: avp, 2012-04-22 14:48:21

Here's how you can try

String s = "буквы,символы,цифры и тд и тп";
s = s.replaceAll("[^A-Za-zА-Яа-я0-9]", ""); // удалится все кроме букв и цифр
 6
Author: diman, 2015-10-11 19:04:45

Sit down and read about the lines, instead of writing garbage

String resultString = str.replaceAll("some char", "some other char");

UPD1

What's wrong with my option? what prevents you from putting signs instead of letters/numbers as the first parameter?

The code may not work =)

public class HelloWorld {
    public static void main (String... av) {

        String s = "  --abc  678 .. Z";

        System.out.println("End "+delNoDigOrLet(s));
    }

    private static String delNoDigOrLet (String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isLetterOrDigit(s.charAt(i)))
              String str = s.replaceAll(str.charAt(i), "");

        }
        return str;
    }
}

Well, or it is better to store the "forbidden" characters in some array-dictionary and replace them in the string with the same search.

 3
Author: Gorets, 2012-04-22 16:49:51

Hi, I myself faced the problem of how to remove all the letters from the string and leave only the numbers. It was this topic that prompted the answer :) My modification at the request of the author:

import java.util.Scanner;
public class test {

    public static void main(String[] args) {
        int n = 0;
        String textDigits = "";
        Scanner sc = new Scanner(System.in);
        System.out.print("Введите свой текст: ");
        String self = sc.nextLine();

        for (char ch : self.toCharArray()) {
            if ( Character.isLetterOrDigit(ch) ) {
                textDigits = textDigits + ch;
            }else{
                n++;
            }
        }
        System.out.println("Количество других символов: " + n);
        System.out.println( textDigits );
    }
}
 2
Author: viva, 2015-03-31 15:33:04

You can do this : It also removes spaces.

String resultString = str.replaceAll("[^A-Za-zА-Яа-я0-9 ]", "");
 1
Author: Shamon, 2020-07-22 11:46:28

I'm for the functionalka:

private static String delNoDigOrLet(String str) {
    return str.chars()
            .filter(i -> Character.isLetterOrDigit(i))
            .mapToObj(i -> String.valueOf((char) i))
            .collect(Collectors.joining());
}
 1
Author: Дмитрий, 2020-11-03 10:04:48

If you solve the problem using a regular expression, you can use

String result = self.replaceAll("[^\\p{L}\\p{N}]+", "");

If you need to leave whitespace characters:

String result = self.replaceAll("(?U)[^\\p{L}\\p{N}\\s]+", "");

See example of how a regular expression works.

Details

  • (?U) - modifier Pattern.UNICODE_CHARACTER_CLASS (not used in Android!), which in this expression allows \s to find all Unicode whitespace characters, not just ASCII
  • [^ - the beginning of an exclusive (negating) character of the class, it finds any character other than those specified in the class:
    • \p{L} - any Unicode letter
    • \p{N} - any Unicode digit
    • \s - thanks to (?U) finds any Unicode whitespace character
  • ]+ - end of character class, 1 or more repeats (for faster deletion).

See example of how Java code works:

System.out.println("Я – робот123!N".replaceAll("[^\\p{L}\\p{N}]+", ""));
// => Яробот123N
 0
Author: Wiktor Stribiżew, 2020-11-03 09:57:21