Java-Java String转换函数
请问各位大牛这个字符串函数怎么实现。
输入是一个类似的长字符串:
The above solution has the problem of prematurely cutting the text,
if it contains a newline before the actual cutpoint. nnHere a version
which solves this problem.
要求输出一个新字符串: (1) 字符串中的单词如果长度大于3,将进行阶段并在后面加上 -rt, 例如above将变成abo-rt (2) 字母大小写保持不变 (3) 标点符号必须保留
请问怎么实现这个函数 public convertString(String str).
非常感谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
public String convertString(String str){
StringBuilder sb=new StringBuilder();
for(String s: str.split("\b")){
if(s.matches("^\w.*") && s.length()>3)
s=s.substring(0,3)+"-rt";
sb.append(s);
}
return sb.toString();
}
@Test
public void testConvertStr(){
String testStr="The above solution has t the problem of prematurely cutting the text, "+
"if it contains a newline before the actual cutpoint. nnHere a version "+
"which solves this problem.";
System.out.println("original string: n "+testStr);
System.out.println("nnconverted string: n "+convertString(testStr));
}
结果:
original string:
The above solution has the problem of prematurely cutting the text, if it contains a newline before the actual cutpoint.
Here a version which solves this problem.
converted string:
The abo-rt sol-rt has the pro-rt of pre-rt cut-rt the tex-rt, if it con-rt a new-rt bef-rt the act-rt cut-rt.
Her-rt a ver-rt whi-rt sol-rt thi-rt pro-rt.