1. <i id='WDqZF'><tr id='WDqZF'><dt id='WDqZF'><q id='WDqZF'><span id='WDqZF'><b id='WDqZF'><form id='WDqZF'><ins id='WDqZF'></ins><ul id='WDqZF'></ul><sub id='WDqZF'></sub></form><legend id='WDqZF'></legend><bdo id='WDqZF'><pre id='WDqZF'><center id='WDqZF'></center></pre></bdo></b><th id='WDqZF'></th></span></q></dt></tr></i><div id='WDqZF'><tfoot id='WDqZF'></tfoot><dl id='WDqZF'><fieldset id='WDqZF'></fieldset></dl></div>
      <legend id='WDqZF'><style id='WDqZF'><dir id='WDqZF'><q id='WDqZF'></q></dir></style></legend>

        <tfoot id='WDqZF'></tfoot>

          <bdo id='WDqZF'></bdo><ul id='WDqZF'></ul>

      1. <small id='WDqZF'></small><noframes id='WDqZF'>

      2. 如何从字符串中取出数字?

        How to get numbers out of string?(如何从字符串中取出数字?)
            <tbody id='pEPBp'></tbody>
          <tfoot id='pEPBp'></tfoot>

          <small id='pEPBp'></small><noframes id='pEPBp'>

        • <legend id='pEPBp'><style id='pEPBp'><dir id='pEPBp'><q id='pEPBp'></q></dir></style></legend>
            • <bdo id='pEPBp'></bdo><ul id='pEPBp'></ul>

              1. <i id='pEPBp'><tr id='pEPBp'><dt id='pEPBp'><q id='pEPBp'><span id='pEPBp'><b id='pEPBp'><form id='pEPBp'><ins id='pEPBp'></ins><ul id='pEPBp'></ul><sub id='pEPBp'></sub></form><legend id='pEPBp'></legend><bdo id='pEPBp'><pre id='pEPBp'><center id='pEPBp'></center></pre></bdo></b><th id='pEPBp'></th></span></q></dt></tr></i><div id='pEPBp'><tfoot id='pEPBp'></tfoot><dl id='pEPBp'><fieldset id='pEPBp'></fieldset></dl></div>

                1. 本文介绍了如何从字符串中取出数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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.

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

                  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.

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

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

                  目前基本的streamTokenizer代码是:

                  The basic streamTokenizer code at present is:

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

                  或者有人可以建议使用 REGEXP 来实现这一目标吗?我不确定 REGEXP 在这里是否有用,因为在从字符串中读取标记后会进行任何处理.

                  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.

                  谢谢

                  摩根先生.

                  推荐答案

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

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

                      String s = "$23.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;
                      }
                  

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

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

                  这篇关于如何从字符串中取出数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                2. <small id='BHXGv'></small><noframes id='BHXGv'>

                      • <bdo id='BHXGv'></bdo><ul id='BHXGv'></ul>
                      • <legend id='BHXGv'><style id='BHXGv'><dir id='BHXGv'><q id='BHXGv'></q></dir></style></legend>
                        <i id='BHXGv'><tr id='BHXGv'><dt id='BHXGv'><q id='BHXGv'><span id='BHXGv'><b id='BHXGv'><form id='BHXGv'><ins id='BHXGv'></ins><ul id='BHXGv'></ul><sub id='BHXGv'></sub></form><legend id='BHXGv'></legend><bdo id='BHXGv'><pre id='BHXGv'><center id='BHXGv'></center></pre></bdo></b><th id='BHXGv'></th></span></q></dt></tr></i><div id='BHXGv'><tfoot id='BHXGv'></tfoot><dl id='BHXGv'><fieldset id='BHXGv'></fieldset></dl></div>

                      • <tfoot id='BHXGv'></tfoot>

                            <tbody id='BHXGv'></tbody>