本帖最后由 马胜平 于 2012-2-26 14:47 编辑
下面是我写的代码用正则表达式,测试能行 供参考(没有考虑以数字开头或其他特殊字符情况):- public class Test {
- public static void main(String[]args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, Exception{
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
- StringBuilder sbuider=new StringBuilder();
- String line=null;
- String line2=null;
-
- while((line=br.readLine())!=null){
-
- Pattern p2=Pattern.compile("([a-zA-Z])+(\\d+)([a-zA-Z])+");//正则表达式匹配前面是字符中间是数字后面是字母
- Matcher m2=p2.matcher(line);
- //把有数字的地方替换成_字母_的形式
- while(m2.find()){
- line2=line.replaceAll(m2.group(2), "_"+m2.group(2)+"_");//将数值前后加上下划线,这里用到分组
- }
- Pattern p1=Pattern.compile("\\s+");
- //按空白符号把字符串分为数组
- String[]arr=p1.split(line2);
- for(String s:arr){
- s=s.substring(0,1).toUpperCase()+s.substring(1,s.length());
- sbuider.append(s);
- sbuider.append(" ");//添加一个空白符号
-
- }
- line2=sbuider.toString();
- bw.write(line2);
-
- bw.flush();
-
- }
-
-
-
-
-
-
- }
- }
复制代码 |