轉:
java從字符串中提取數字
- // 判斷一個字符串是否都為數字
- 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 "";
- }