我们如何从Java中的输入字符串中提取数字?
该java.lang.String中 类提供的方法有相当的措施来处理字符串。借助这些方法,您可以对字符串执行诸如修整,连接,转换和比较之类的操作。我们可以使用String 类的replaceAll() 方法从给定的字符串中提取数字。
示例
import java.util.Scanner;
public class StringExtractTest {
public static void main(String[] args) {
String input;
String numbers;
Scanner scanner = new Scanner(System.in);
System.out.print(" Please enter a string from the keyboard that contains numbers: ");
input = scanner.nextLine(); numbers = input.replaceAll("[^0-9]", "");
System.out.println("The Numbers are: " + numbers);
}
}输出结果
Please enter a string from the keyboard that contains numbers: Welcome to nhooo.com 1234567890The Numbers are: 1234567890
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短