如何将子字符串串联重构为StringBuilder
?
String line = "this is my start and my end string";
int start = line.indexOf("start");
line = line.substring(0, start) + line.substring(start + 5);
int end = line.indexOf("end");
line = line.substring(0, end) + line.substring(end + 3);
//result: this is my and my string";
使用StringBuilder,相同的代码看起来如何?当我想一次又一次地执行子字符串几次时?
如果您的问题是“如何反复从字符串中删除子字符串”,则可以尝试以下操作:
StringBuilder line = new StringBuilder("this is my start and my end string");
int start = line.indexOf("start");
line.delete(start, start + 5);
int end = line.indexOf("end");
line.delete(end, end + 3);
String result = sb.toString()
一次创建StringBuilder,然后操纵其内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句