Java 如何从字符串中取出数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3272575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:14:32  来源:igfitidea点击:

How to get numbers out of string?

javastringparsingnumbersstringtokenizer

提问by Mr Morgan

I'm using a Java StreamTokenizer to extract the various words and numbers of a String but have run into a problem where numbers which include commas are concerned, e.g. 10,567 is being read as 10.0 and ,567.

我正在使用 Java StreamTokenizer 来提取字符串的各种单词和数字,但遇到了一个问题,其中涉及包含逗号的数字,例如 10,567 被读取为 10.0 和 ,567。

I also need to remove all non-numeric characters from numbers where they might occur, e.g. $678.00 should be 678.00 or -87 should be 87.

我还需要从可能出现的数字中删除所有非数字字符,例如 $678.00 应该是 678.00 或 -87 应该是 87。

I believe these can be achieved via the whiteSpace and wordChars methods but does anyone have any idea how to do it?

我相信这些可以通过 whiteSpace 和 wordChars 方法来实现,但有没有人知道怎么做?

The basic streamTokenizer code at present is:

目前基本的streamTokenizer代码是:

        BufferedReader br = new BufferedReader(new StringReader(text));
        StreamTokenizer st = new StreamTokenizer(br);
        st.parseNumbers();
        st.wordChars(44, 46); // ASCII comma, - , dot.
        st.wordChars(48, 57); // ASCII 0 - 9.
        st.wordChars(65, 90); // ASCII upper case A - Z.
        st.wordChars(97, 122); // ASCII lower case a - z.
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            if (st.ttype == StreamTokenizer.TT_WORD) {                    
                System.out.println("String: " + st.sval);
            }
            else if (st.ttype == StreamTokenizer.TT_NUMBER) {
                System.out.println("Number: " + st.nval);
            }
        }
        br.close(); 

Or could someone suggest a REGEXP to achieve this? I'm not sure if REGEXP is useful here given that any parding would take place after the tokens are read from the string.

或者有人可以建议使用 REGEXP 来实现这一目标吗?我不确定 REGEXP 在这里是否有用,因为在从字符串中读取令牌后会发生任何parding。

Thanks

谢谢

Mr Morgan.

摩根先生。

采纳答案by Denis Tulskiy

StreamTokenizer is outdated, is is better to use Scanner, this is sample code for your problem:

StreamTokenizer 已过时,最好使用Scanner,这是您问题的示例代码:

    String s = ".24 word -123";
    Scanner fi = new Scanner(s);
    //anything other than alphanumberic characters, 
    //comma, dot or negative sign is skipped
    fi.useDelimiter("[^\p{Alnum},\.-]"); 
    while (true) {
        if (fi.hasNextInt())
            System.out.println("Int: " + fi.nextInt());
        else if (fi.hasNextDouble())
            System.out.println("Double: " + fi.nextDouble());
        else if (fi.hasNext())
            System.out.println("word: " + fi.next());
        else
            break;
    }

If you want to use comma as a floating point delimiter, use fi.useLocale(Locale.FRANCE);

如果要使用逗号作为浮点分隔符,请使用 fi.useLocale(Locale.FRANCE);

回答by gorn

Sure this can be done with regexp:

当然这可以用正则表达式来完成:

s/[^\d\.]//g

However notice that it eats all commas, which is probably what you want if using american number format where comma is only separating thousands. In some languages comma is used instead of the point as a decimal separator. So take care when parsing international data.

但是请注意,它会占用所有逗号,如果使用逗号仅分隔数千个的美国数字格式,这可能是您想要的。在某些语言中,使用逗号代替点作为小数点分隔符。所以在解析国际数据时要小心。

I leave it on you to translate this to Java.

我让你把它翻译成 Java。

回答by Carl Smotricz

Try this:

尝试这个:

String sanitizedText = text.replaceAll("[^\w\s\.]", "");

SanitizedText will contain only alphanumerics and whitespace; tokenizing it after that should be a breeze.

SanitizedText 将只包含字母数字和空格;在那之后标记它应该是轻而易举的。

EDIT

编辑

Edited to retain the decimal point as well (at the end of the bracket). .is "special" to regexp so it needs a backslash escape.

编辑以保留小数点(在括号的末尾)。.正则表达式是“特殊的”,所以它需要一个反斜杠转义。

回答by ankushb

    String str = "1,222";
    StringBuffer sb = new StringBuffer();
    for(int i=0; i<str.length(); i++)
    {
        if(Character.isDigit(str.charAt(i)))
            sb.append(str.charAt(i));
    }
    return sb.toString()

回答by mordekhai

This worked for me :

这对我有用:

String onlyNumericText = text.replaceAll("\\D", "");

回答by Anuj Dhiman

Code for get numbers from string.For example i have string "123" then i want to number 123.

从字符串中获取数字的代码。例如,我有字符串“123”,然后我想编号 123。

    int getNumber(String str){
            int i=0;
            int num=0;
            int zeroAscii = (int)'0';
            while (i<str.length()) {
                int charAscii=(int)str.charAt(i);
                num=num*10+(charAscii-zeroAscii);
                 i++;
                  }   
            return num;
        }

Source : How to get number from string

来源:如何从字符串中获取数字