“字符串小问题”
我想让他变成“ 字  符  串  小  问  题 ”
也就是每个字符前后都加上个空格
不知道实现MessageFormat接口的类能不能实现这种情况
还有政则表达式行么?
还有没有其他的解决方法
一个字符一个字符的读出来再加空格可不是一个好办法

解决方案 »

  1.   

    "字符串小".replaceAll(""," ");
      

  2.   

    String s = "This is a test case.";
    System.out.println(s.replaceAll("(\\w)"," $1 "));
      

  3.   

    dreamno(披着狼皮的羊)  这位老兄的方法就很好呀!
      

  4.   

    那我就用MessageFormat来实现
    String strItem = "字符串小";
    String[] arguments = strItem.split("");
    String strPattern = " ";
    for (int i=0 ;i<arguments.length ; ++i){
       strPattern += "{"+i+"} ";
    }
    String result = java.text.MessageFormat.format(strPattern,arguments);
    System.out.println("result:"+result);
      

  5.   

    真是N多的问题,什么奇怪的需要都有
    humanity的方法不错啊
    String s = "This is a test case.";
    System.out.println(s.replaceAll("(\\w)"," $1 "));
      

  6.   

    看了大家说的再去看看jdk帮助文档,帮助挺大-----窃喜中
    楼上的我在看了之后看见这样一句话,我不是很看的懂,但我觉得应该对你有帮助。
    Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
      

  7.   

    String s = "字符串小问题.";
    System.out.println(s.replaceAll("(\\w)"," $1 "));中文不行啊
      

  8.   

    System.out.println(s.replaceAll("(\\w)"," $1 "));
    用在中文都不行啊
    replaceAll(""," ");
    这个都可以啊
      

  9.   

    不过有点不大清楚""为何可以看做一个字符,那其中是表示为NULL还是什么,请高手指示
      

  10.   

    String s = "字符串小问题.";
    System.out.println(s.replaceAll("([\\w|\\u4e00-\\u9fa5])"," $1 "));
      

  11.   

    向 laughsmile(海边的星空) 学习
      

  12.   

    我试了
    String s = "字符串小问题.";
    System.out.println(s.replaceAll("(\\w)"," $1 "));
    可以阿
      

  13.   

    怎么可能可以呢?
    String s = "字符串小问题.";
    s =s.replaceAll("(\\w)"," $1 ");
    System.out.println("First Time ReplaceAll Result: "+s);
    System.out.println(s.length());
    System.out.println("_______________________");
    s =s.replaceAll("([\\w|\\u4e00-\\u9fa5])"," $1 ");
    System.out.println("Second Time ReplaceAll Result: "+s);
    System.out.println(s.length());
    我的运行结果:
    First Time ReplaceAll Result: 字符串小问题.
    7
    _______________________
    Second Time ReplaceAll Result:  字  符  串  小  问  题 .
    19
    如果你的运行结果真的是可以,请告知我.