请问各位大牛这个字符串函数怎么实现。输入是一个类似的长字符串: 
The above solution has the problem of prematurely cutting the text, 
if it contains a newline before the actual cutpoint. \n\nHere a version 
which solves this problem.要求输出一个新字符串: (1) 字符串中的单词如果长度大于3,将进行阶段并在后面加上 -rt, 例如above将变成
abo-rt (2) 字母大小写保持不变 (3) 标点符号必须保留请问怎么实现这个函数 public stringConvert(String str).非常感谢。Java 算法

解决方案 »

  1.   

    package practice;public class Test {
    public static void main(String[] args) {
    String str = "OK cnmdfg, dsfsd sddsf. dsfsd sdf.";
    String[] strs = str.split(" ");
    String str1 = "";
    String str2 = "";
    for (int i = 0; i < strs.length; i++) {
    if(strs[i].length()>3){
    str2 = strs[i].substring(0,3)+"-rt"+strs[i].substring(3,strs[i].length());
    }else{
    str2=strs[i];
    }
    str1 += str2+" ";
    }
    System.out.println(str1);
    }
    }
      

  2.   

    split还是比较麻烦的。
    如果正则会方便很多 public static void main(String[] args) throws Exception {
          String str="The above solution has the problem of prematurely cutting the text, if it contains a newline before the actual cutpoint. \n\nHere a version which solves this problem.";
          str=str.replaceAll("(?s)(\\w{1,3})[^\\w]", "$1-tr ");
          System.out.println(str);
        }
      

  3.   

    The-tr above-tr solution-tr has-tr the-tr problem-tr of-tr prematurely-tr cutting-tr the-tr text-tr  if-tr it-tr contains-tr a-tr newline-tr before-tr the-tr actual-tr cutpoint-tr  Here-tr a-tr version-tr which-tr solves-tr this-tr problem-tr 
    正则 结果 是这样啊  跟 楼主 要的 结果 不同 啊。。
      

  4.   

    如果输入是"Welcome to the Test!"
    理想的输出结果是这样的 “Wel-rt to the Tes-rt!”