java String 类

来源:百度知道 编辑:UC知道 时间:2024/05/28 05:37:33
请高手演示一下,String 类的 String.matches() 和 String.format()这两个方法是怎么使用的。

(参数我没写上来,因为string 类没有同名的方法)

public boolean matches(String regex)
通知此字符串是否匹配给定的正则表达式。
此方法调用的 str.matches(regex) 形式与以下表达式产生完全相同的结果: Pattern.matches(regex, str)

public static String format(String format, Object... args)
JDK1.5新增的方法,使用指定的格式字符串和参数返回一个格式化字符串。
始终使用的语言环境是由 Locale.getDefault() 返回的语言环境。

举例:

// 判断给定的字符串中是否包含数字
System.out.println("Hello World!".matches("\\d"));

// 将字符串的格式符替换成指定的数值
System.out.println(String.format("请输入 %d 到 %d 之间的一个数", 1, 100));