请问各位大牛这个字符串函数怎么实现。输入是一个类似的长字符串:
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 算法
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 算法
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);
}
}
如果正则会方便很多 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);
}
正则 结果 是这样啊 跟 楼主 要的 结果 不同 啊。。
理想的输出结果是这样的 “Wel-rt to the Tes-rt!”