解析springmvc工作流程 stringbuffer截取字符串的下标( 二 )


StringBuilder和StringBuffer的append
其示例代码如下所示:
public class StringTest {public static void main(String[] args) throws IOException {// 可以拼接所有的基本数据类型StringBuilder strBuilder = new StringBuilder();StringBuffer strBuffer = new StringBuffer();// 拼接int(byte、short都可以自动转换为int)strBuilder.append(12).append(",");strBuffer.append(12).append(",");// 拼接longstrBuilder.append(13L).append(",");strBuffer.append(13L).append(",");// 拼接floatstrBuilder.append(3.4f).append(",");strBuffer.append(3.4f).append(",");// 拼接doublestrBuilder.append(3.5).append(",");strBuffer.append(3.5).append(",");// 拼接字符数组strBuilder.append("hello".toCharArray()).append(",");strBuffer.append("hello".toCharArray()).append(",");// 拼接其他引用对象strBuilder.append(new Date()).append(",");strBuffer.append(new Date()).append(",");// 拼接指定字符数组偏移指定位数后的指定长度字符strBuilder.append("hello".toCharArray(), 2, 2).append(",");strBuffer.append("hello".toCharArray(), 2, 2).append(",");// 拼接指定字符序列对象(常见的为String、StringBuffer和StringBuilder)指定开始和结束(不包括)的字符串strBuilder.append("hello", 1, 3).append(",");strBuffer.append("hello", 1, 3).append(",");printStrBuilder(strBuilder);printStrBuffer(strBuffer);}private static void printStrBuilder(StringBuilder strBuilder) {String[] strArr = strBuilder.deleteCharAt(strBuilder.length() - 1).toString().split(",");System.out.println("StringBuilder信息为:n"Arrays.asList(strArr));}private static void printStrBuffer(StringBuffer strBuffer) {String[] strArr = strBuffer.deleteCharAt(strBuffer.length() - 1).toString().split(",");System.out.println("StringBuffer信息为:n"Arrays.asList(strArr));}} 只想结果如下图所示:
获取某个字符串在另一个字符串中的索引位置这里使用的方法有四个,如下图:
相关的示例代码如下所示:
public class StringTest {public static void main(String[] args) throws IOException {StringBuilder strBuilder = new StringBuilder("no zuo no die no happy no problem");StringBuffer strBuffer = new StringBuffer("no zuo no die no happy no problem");// indexOfSystem.out.println(""no"在strBuilder中首次出现的位置为:"strBuilder.indexOf("no"));System.out.println(""no"在strBuffer中首次出现的位置为:"strBuffer.indexOf("no"));System.out.println(""no"在strBuilder中在索引3之后首次出现的位置为:"strBuilder.indexOf("no", 3));System.out.println(""no"在strBuffer中在索引3之后首次出现的位置为:"strBuffer.indexOf("no", 3));// lastIndexOfSystem.out.println(""no"在strBuilder中最后出现的位置为:"strBuilder.lastIndexOf("no"));System.out.println(""no"在strBuffer中最后出现的位置为:"strBuffer.lastIndexOf("no"));System.out.println(""no"在strBuilder中在索引20之前最后出现的位置为:"strBuilder.lastIndexOf("no", 20));System.out.println(""no"在strBuffer中在索引20之前最后出现的位置为:"strBuffer.lastIndexOf("no", 20));}}

猜你喜欢