我们如何从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 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短