- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- public class Test {
- public static void main(String[] args) throws Exception
- {
- String s="we are at the b2b web 2aT22f5p 2m2";
- s=s.replaceAll(" +"," "); //多个空格只输出一个
- s=s.replaceAll("([A-Za-z])(\\d)","$1_$2"); //左字符+右数字用"_"间隔
- s=s.replaceAll("(\\d)([A-Za-z])","$1_$2"); //左数字+右字符用"_"间隔
- StringBuffer sb=new StringBuffer();
- Pattern p=Pattern.compile("\\b\\w|\\s\\w"); //首字母大写
- Matcher m=p.matcher(s);
- while(m.find()){
- m.appendReplacement(sb,m.group().toUpperCase());
- }
- m.appendTail(sb);
- System.out.println(sb);
- }
- }
复制代码 这个估计用JavaScript 反而简单些。 |