How to get numbers out of string?(如何从字符串中取出数字?)
问题描述
我正在使用 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);
这篇关于如何从字符串中取出数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从字符串中取出数字?


基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01