1 methods provided by the String class:
?
1234567891011121314151617181920212223242526272829 |
package 测试练习;
import Java.util.*;
public class get_StringNum {
/**
*2016.10.25
*/
public static void main(String[] args) {
String str =
"love23next234csdn3423javaeye"
;
str=str.trim();
String str2=
""
;
if
(str !=
null && !
""
.equals(str)){
for
(
int i=
0
;i<str.length();i++){
if
(str.charAt(i)>=
48 && str.charAt(i)<=
57
){
str2+=str.charAt(i);
}
}
}
System.out.println(str2);
}
}
output:
232343423
|
This method has an obvious disadvantage, can only extract the numbers together, can not be extracted separately. Of course it can be improved, interested friends can try.
2 Regular Expressions
?
1234567891011121314151617181920212223 |
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class get_StringNum { /**
*2016.10.25
*/
public static void main(String[] args) {
String a=
"love23next234csdn3423javaeye"
;
String regEx=
"[^0-9]"
;
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(a);
System.out.println( m.replaceAll(
""
).trim());
}
}
output:
232343423
|
Pattern, Matcher is the two classes in the Java.util.regex software package, you can consult the API for specific usage. It is also not possible to extract numbers individually.
3 Collection Class Library
?
1234567891011121314151617181920212223242526272829 |
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class get_StringNum {
/**
*2016.10.25
*/
public static void main(String[] args) {
String a=
"love23next234csdn3423javaeye"
;
List<String> digitList =
new ArrayList<String>();
Pattern p = Pattern.compile(
"[^0-9]"
);
Matcher m = p.matcher(a);
String result = m.replaceAll(
""
);
for (
int i =
0
; i < result.length(); i++) {
digitList.add(result.substring(i, i+
1
));
}
System.out.println(digitList);
}
}
output:
[
2
,
3
,
2
,
3
,
4
,
3
,
4
,
2
,
3
]
|
The same idea:
?
123456789101112131415161718192021222324252627 |
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class get_StringNum {
/**
*2016.10.25
*/
public static void main(String[] args) {
String a=
"love23next234csdn3423javaeye"
;
List<String> ss =
new ArrayList<String>();
for
(String sss:s.replaceAll(
"[^0-9]"
,
","
).split(
","
)){
if (sss.length()>
0
)
ss.add(sss);
}
System.out.print(ss);
}
}
output:
[
2
,
3
,
2
,
3
,
4
,
3
,
4
,
2
,
3
]
|
Obviously, we can extract the numbers separately using regular expressions.
There is also an answer that is found using the lookup document, as follows:
?
12345678910111213141516171819202122232425262728293031323334353637383940 |
/**
* 从字符串文本中获得数字 *@param
text
*@return
*/
publicstatic
List<Long>
getDigit(String text) {
List<Long>
digitList =
new
ArrayList<Long>();
Pattern p=
Pattern.compile(
"(\\d+)"
);
Matcher m=
p.matcher(text);
while
(m.find()) { String find=
m.group(
1
).toString();
digitList.add(Long.valueOf(find));
}
return
digitList;
}
|
Two methods for judging the matching of regular expressions are as follows;
?
12345678910111213141516171819202122232425262728293031 |
// 判断一个字符串是否都为数字
public boolean isDigit(String strNum) {
return strNum.matches(
"[0-9]{1,}"
);
}
// 判断一个字符串是否都为数字
public boolean isDigit(String strNum) {
Pattern pattern = Pattern.compile(
"[0-9]{1,}"
);
Matcher matcher = pattern.matcher((CharSequence) strNum);
return matcher.matches();
}
//截取数字
public String getNumbers(String content) {
Pattern pattern = Pattern.compile(
"\\d+"
);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
return matcher.group(
0
);
}
return ""
;
}
// 截取非数字
public String splitNotNumber(String content) {
Pattern pattern = Pattern.compile(
"\\D+"
);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
return matcher.group(
0
);
}
return ""
;
}
|
Java extracts numbers from a string