[英]Java: Delimiters and regular expressions
I'm using the scanner method to do work on a string and need to filter out junk 我正在使用扫描仪方法对字符串进行处理,并且需要过滤掉垃圾
here's the sample string 这是示例字符串
5/31/1948@14:57
I need to strip out the / @ : 我需要删除/ @:
Theres this doc: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html 有此文档: http : //download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
But it's really confusing. 但这确实令人困惑。
You can use the replaceAll method as: 您可以将replaceAll方法用作:
String filetredStr = inputStr.replaceAll("[@/:]","");
And if you want to delete any non-digit you can do: 如果要删除任何非数字,可以执行以下操作:
String filetredStr = inputStr.replaceAll("[^0-9]","");
如果您想将其拆分,请使用String#split()
String[] parts = "5/31/1948@14:57".split("[/@:]");
做这样的事情:
s.replaceAll("[\\/@:]", "");
An alternative to replaceAll(a,b)
is as follows: replaceAll(a,b)
的替代方法如下:
String str = "5/31/1948@14:57";
String charsToRemove = "/@:";
for (int i = 0; i < charsToRemove.length(); i++) {
str = str.replace(charsToRemove.charAt(i)+"", "");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.