★11.字符串

2017-07-03  本文已影响0人  iDragonfly

String

StringBuilder

简介

相关方法

正则表达式

规则





简单示例

代码

String reg = "[a-z]";
String str = "abcZde";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.print("Match\"" + m.group() + "\" at positions "
            + m.start() + "-" + (m.end() - 1) + "\n");
}

结果

Match"a" at positions 0-0
Match"b" at positions 1-1
Match"c" at positions 2-2
Match"d" at positions 4-4
Match"e" at positions 5-5

Matcher

String s = "aeiou AEIOU";
StringBuffer sbuf = new StringBuffer();
Pattern p = Pattern.compile("[aeiou]");
Matcher m = p.matcher(s);
while (m.find()) {
    m.appendReplacement(sbuf, m.group().toUpperCase());
}
m.appendTail(sbuf);
System.out.print(sbuf + "\n");

Pattern

Scanner

上一篇 下一篇

猜你喜欢

热点阅读