Java: removing numeric values from string
Asked Answered
C

7

30

I have suceeded with the help of this community in removing numeric values from user input, however, my code below will only retrieve the alpha characters before the numeric that has been removed:

import java.util.Scanner;

public class Assignment2_A {

    public static void main(String[] args) {
        Scanner firstname = new Scanner(System.in);
        String firstname1 = firstname.next();
        firstname1 = firstname1.replaceAll("[^A-Z]","");
        System.out.println(firstname1);
    }
}

For example if user input = S1234am, I am only getting back: S. How do I retrieve the remaining characters in the string?

Complexion answered 7/7, 2013 at 20:26 Comment(1)
u need to replace lowercase letters as wellSteve
A
20

Your regular expression [^A-Z] is currently only configured to preserve upper-case letters. You could try replacing it with [^A-Za-z] to keep the lower-case letters too.

Albany answered 7/7, 2013 at 20:28 Comment(2)
Indeed. If the goal is really to remove all digits, then using \\d would be better, but the way it was currently designed suggested the goal might be to just preserve A-Za-z.Albany
Gian, Thank you for your help. I would give you a thumbs up but apparently I am too new on this site to do so. Thank you for your time!Complexion
W
65

This will remove all digits:

firstname1 = firstname1.replaceAll("\\d","");
Winwaloe answered 7/7, 2013 at 20:28 Comment(0)
F
26

You can use:

firstname1 = firstname1.replaceAll("[0-9]","");

This will remove all numeric values from String firstName1.

    String firstname1 = "S1234am";
    firstname1 = firstname1.replaceAll("[0-9]","");
    System.out.println(firstname1);//Prints Sam
Fritzie answered 7/7, 2013 at 20:32 Comment(2)
This answer is more relevant to the question as using [^A-Za-z] will also remove white spaces and any other values apart from the numeric values.Phalanstery
I think this is the best answer. The others above are (some) ok, save the -1, but this one is best, IMO, because it is short, concise, clear, and quite to the point. The range specified 0-9 is also what other languages typically use.Priestess
A
20

Your regular expression [^A-Z] is currently only configured to preserve upper-case letters. You could try replacing it with [^A-Za-z] to keep the lower-case letters too.

Albany answered 7/7, 2013 at 20:28 Comment(2)
Indeed. If the goal is really to remove all digits, then using \\d would be better, but the way it was currently designed suggested the goal might be to just preserve A-Za-z.Albany
Gian, Thank you for your help. I would give you a thumbs up but apparently I am too new on this site to do so. Thank you for your time!Complexion
G
7

How to remove numeric values from a string:

to do this it will be enough

str.replaceAll("[^A-Za-z]","");

but what if your string contain characters like:

String str = "stackoverflow elenasys +34668555555 # Пивоварова Пивоварова հայեր հայեր አማሪኮ     አማሪኮ kiểm tra kiểmtra ตรวจสอบ ตรวจสอบ التحقق من التحقق من";

most of the characters will be removed too, so this is a better option:

str = str.replaceAll("[\\d.]", "");

to remove all numeric values and get as result:

stackoverflow elenasys + # Пивоварова Пивоварова հայեր հայեր አማሪኮ     አማሪኮ kiểm tra kiểmtra ตรวจสอบ ตรวจสอบ التحقق من التحقق من
Gentlefolk answered 25/9, 2017 at 21:40 Comment(0)
B
1

Your regex:

[^A-Z]

matches anything which is not an uppercase letter.

Which means any lowercase letter will match too.

You should probably use:

[^A-Za-z]

as a regex instead.

Note also that this will not account for anything other than ASCII. It may, or may not, be what you want.

Baltazar answered 7/7, 2013 at 20:29 Comment(0)
K
0
public static void main(String[] args) {
    String address = "34732483dhshdsdhajsa8ejdsdd";
    char[] chars = address.toCharArray();
    String aString = "";

    for (int i = 0; i < chars.length; i++) {
        if (!Character.isDigit(chars[i])) {
            aString =aString + chars[i]; 


        }

    }System.out.println(aString);



}
Kawai answered 28/2, 2019 at 17:14 Comment(0)
F
-1
/*Remove numbers from given specific string*/
public class NewClass6 {

    public static void main(String[] args){
        String s= "hello647hi74joke";
        char[] ch= s.toCharArray();
        System.out.println("Result = " + getString(ch));
    }

    static String getString(char[] ch){
        int m = 0;
        char[] chr = new char[50];
        char[] k = {'0','1','2','3','4','5','6','7','8','9'};

        for(int i = 0; i < ch.length; i++){
           for(int j = 0; j < k.length; j++){  
               if(ch[i]==k[j]){
                   m--;
                   break;
               }
               else {
                   chr[m]=ch[i];
               }
            }
            m++;
         }

         String st = String.valueOf(chr);
         return st;
     }
}
Flowerer answered 15/3, 2018 at 13:45 Comment(1)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.Crosspurpose

© 2022 - 2024 — McMap. All rights reserved.