Java如何从StringBuilder中删除子字符串?
本示例演示如何使用StringBuilderdelete(intstart,intend)和deleteCharAt(intindex)从中删除子字符串或单个字符StringBuilder。
package org.nhooo.example.lang; public class StringBuilderDelete { public static void main(String[] args) { StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit " + "amet, consectetur adipisicing elit."); System.out.println("lipsum = " + lipsum.toString()); //我们将从此StringBuilder中删除一个子字符串,从 //第一个字符到第28个字符。 lipsum.delete(0, 28); System.out.println("lipsum = " + lipsum.toString()); //从StringBuilder中删除一个字符。在下面的示例中,我们 //删除最后一个字符。 lipsum.deleteCharAt(lipsum.length() - 1); System.out.println("lipsum = " + lipsum.toString()); } }
上面的代码片段的结果:
lipsum = Lorem ipsum dolor sit amet, consectetur adipisicing elit. lipsum = consectetur adipisicing elit. lipsum = consectetur adipisicing elit