简体   繁体   English

Java:从字符串中删除数值

[英]Java: removing numeric values from string

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?例如,如果用户输入 = S1234am,我只会返回:S。如何检索字符串中的剩余字符?

这将删除所有数字:

firstname1 = firstname1.replaceAll("\\d","");

You can use:您可以使用:

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

This will remove all numeric values from String firstName1 .这将从 String firstName1删除所有数值。

    String firstname1 = "S1234am";
    firstname1 = firstname1.replaceAll("[0-9]","");
    System.out.println(firstname1);//Prints Sam

Your regular expression [^AZ] is currently only configured to preserve upper-case letters.您的正则表达式[^AZ]当前仅配置为保留大写字母。 You could try replacing it with [^A-Za-z] to keep the lower-case letters too.您可以尝试将其替换为[^A-Za-z]以保留小写字母。

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 ตรวจสอบ ตรวจสอบ التحقق من التحقق من

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.另请注意,这不会考虑 ASCII 以外的任何内容。 It may, or may not, be what you want.它可能是您想要的,也可能不是。

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);



}
/*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;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM