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


Java中表示字符串的有三个类:String、StringBuffer和StringBuilder。其中,String的长度是不可变的,而StringBuffer和StringBuilder是长度可变的。对于StringBuffer和StringBuilder来说,两者的API几乎一模一样,因为两者有共同的父类:AbstractStringBuilder。区别在于StringBuffer的大部分方法都是被synchronized关键字修饰,是线程安全的,而StringBuilder则与其相反。

所以本篇就对StringBuffer和StringBuilder类比着来进行介绍。

  • 构造器

StringBuffer和StringBuilder的构造器都有三个,分别如下:

StringBuffer的构造器:

  1. new StringBuffer(int capacity)
  2. new StringBuffer(String str)
  3. new StringBuffer(CharSequence seq)

StringBuilder的构造器:

  1. new StringBuilder(int capacity)
  2. new StringBuilder(String str)
  3. new StringBuilder(CharSequence seq)

对比两者构造器发现,它们对构造器的重载都施行了同样的方式,可以传入一个整数的容量,可以传一个字符串值,甚至可以传一个字符序列的对象。对于Java来说,常用的字符序列对象就是我们认知中的String, StringBuffer和StringBuilder。关于StringBuilder和StringBuffer的使用的示例代码如下所示:

public class StringTest {

    public static void main(String[] args) throws IOException {

        // 初始化一个容量为16,但没有值的空StringBuffer对象
        StringBuffer strBuffer = new StringBuffer(16);
        System.out.println("strBuffer:" + strBuffer + ", 容量为:" + strBuffer.capacity() + ", 长度为:"
                + strBuffer.length());
        // 初始化一个容量为16,但没有值的空StringBuilder对象
        StringBuilder strBuilder = new StringBuilder(16);
        System.out.println("strBuilder:" + strBuilder + ", 容量为:" + strBuilder.capacity() + ", 长度为:"
                + strBuilder.length());

        // 根据一个字符串初始化一个StringBuffer对象
        StringBuffer strBuffer2 = new StringBuffer("how are you");
        System.out.println("strBuffer2的值为:" + strBuffer2 + ", 容量为:" + strBuffer2.capacity() + ", 长度为:"
                + strBuffer2.length());
        // 根据一个字符串初始化一个StringBuilder对象
        StringBuilder strBuilder2 = new StringBuilder("how old are you");
        System.out.println("strBuilder2的值为:" + strBuilder2 + ", 容量为:" + strBuilder2.capacity() + ", 长度为:"
                + strBuilder2.length());

        // 根据一个字符序列对象如StringBuilder来初始化一个StringBuffer对象
        StringBuilder strBuffer3 = new StringBuilder(strBuilder);
        System.out.println("strBuffer3的值为:" + strBuffer3 + ", 容量为:" + strBuffer3.capacity() + ", 长度为:"
                + strBuffer3.length());
        //  根据一个字符序列对象如StringBuffer来初始化一个StringBuilder对象
        StringBuilder strBuilder3 = new StringBuilder(strBuffer2);
        System.out.println("strBuilder3的值为:" + strBuilder3 + ", 容量为:" + strBuilder3.capacity() + ", 长度为:"
                + strBuilder3.length());
    }
}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • 拼接参数

拼接参数常用的方法方法如下图:

Java中字符串StringBuffer和StringBuilder的使用

也就是说,该方法可以拼接所有的基本数据类型和其对应的包装类型,字符数组、字符序列对象及其他的引用对象等。

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(",");
        // 拼接long
        strBuilder.append(13L).append(",");
        strBuffer.append(13L).append(",");
        // 拼接float
        strBuilder.append(3.4f).append(",");
        strBuffer.append(3.4f).append(",");
        // 拼接double
        strBuilder.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));
    }
}

只想结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • 获取某个字符串在另一个字符串中的索引位置

这里使用的方法有四个,如下图:

Java中字符串StringBuffer和StringBuilder的使用

相关的示例代码如下所示:

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");

        // indexOf
        System.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));

        // lastIndexOf
        System.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));

    }


}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • 插入方法

插入方法为:insert(int offset, XXX xxx), 目的是在偏移offset个字符后插入xxx。这里的xxx表示所有的基本数据类型及其对应的包装类型、字符数组、字符序列对象和其他的引用对象等。常用方法如下:

Java中字符串StringBuffer和StringBuilder的使用

​相关示例代码如下所示:

public class StringTest {

    public static void main(String[] args) {

        StringBuilder strBuilder = new StringBuilder("hello");
        StringBuffer strBuffer = new StringBuffer("hello");
        System.out.println("在strBuilder偏移2位后插入整数3的结果为:" + strBuilder.insert(2, 3));
        System.out.println("在strBuffer偏移3位后插入整数3的结果为:" + strBuffer.insert(3, 3));

               StringBuilder strBuilder2 = new StringBuilder("world");
        StringBuffer strBuffer2 = new StringBuffer("world");
        System.out.println("在strBuilder2偏移2位后插入'好'的结果为:" + strBuilder2.insert(2, '好'));
        System.out.println("在strBuffer2偏移3位后插入'好'的结果为:" + strBuffer2.insert(3, '好'));

        StringBuilder strBuilder3 = new StringBuilder("what");
        StringBuffer strBuffer3 = new StringBuffer("what");
        System.out.println("在strBuilder3偏移2位后插入4L的结果为:" + strBuilder3.insert(2, 4L));
        System.out.println("在strBuffer3偏移3位后插入4L的结果为:" + strBuffer3.insert(3, 4L));

        StringBuilder strBuilder4 = new StringBuilder("where");
        StringBuffer strBuffer4 = new StringBuffer("where");
        System.out.println("在strBuilder4偏移2位后插入3.14F的结果为:" + strBuilder4.insert(2, 3.14F));
        System.out.println("在strBuffer4偏移3位后插入3.14F的结果为:" + strBuffer4.insert(3, 3.14F));

        StringBuilder strBuilder5 = new StringBuilder("when");
        StringBuffer strBuffer5 = new StringBuffer("when");
        System.out.println("在strBuilder5偏移2位后插入1.414的结果为:" + strBuilder5.insert(2, 1.414));
        System.out.println("在strBuffer5偏移3位后插入1.414的结果为:" + strBuffer5.insert(3, 1.414));

        StringBuilder strBuilder6 = new StringBuilder("crazy");
        StringBuffer strBuffer6 = new StringBuffer("crazy");
        System.out.println("在strBuilder6偏移2位后插入true的结果为:" + strBuilder6.insert(2, true));
        System.out.println("在strBuffer6偏移3位后插入false的结果为:" + strBuffer6.insert(3, false));

        StringBuilder strBuilder7 = new StringBuilder("hehe");
        StringBuffer strBuffer7 = new StringBuffer("hehe");
        System.out.println("在strBuilder7偏移2位后插入Date的结果为:" + strBuilder7.insert(2, new Date()));
        System.out.println("在strBuffer7偏移3位后插入Date的结果为:" + strBuffer7.insert(3, new Date()));

        StringBuilder strBuilder8 = new StringBuilder("this");
        StringBuffer strBuffer8 = new StringBuffer("this");
        System.out.println("在strBuilder8偏移2位后插入字符数组['a', 'r', 'e']的结果为:"
                + strBuilder8.insert(2, new char[]{'a', 'r', 'e'}));
        System.out.println("在strBuffer8偏移3位后插入字符数组['a', 'r', 'e']的结果为:"
                + strBuffer8.insert(3, new char[]{'a', 'r', 'e'}));

        StringBuilder strBuilder9 = new StringBuilder("happend");
        StringBuffer strBuffer9 = new StringBuffer("happend");
        System.out.println("在strBuilder9偏移2位后插入Date的结果为:"
                + strBuilder9.insert(2, new StringBuffer("234")));
        System.out.println("在strBuffer9偏移3位后插入Date的结果为:"
                + strBuffer9.insert(3, new StringBuilder("234")));

        StringBuilder strBuilder10 = new StringBuilder("that");
        StringBuffer strBuffer10 = new StringBuffer("that");
        System.out.println("在strBuilder10偏移2位后插入字符数组['a', 'r', 'e']的结果为:"
                + strBuilder10.insert(2, new char[]{'a', 'r', 'e'}, 0, 2));
        System.out.println("在strBuffer10偏移3位后插入字符数组['a', 'r', 'e']的结果为:"
                + strBuffer10.insert(3, new char[]{'a', 'r', 'e'}, 0, 2));

        StringBuilder strBuilder11 = new StringBuilder("jerry");
        StringBuffer strBuffer11 = new StringBuffer("jerry");
        System.out.println("在strBuilder11偏移2位后插入Date的结果为:"
                + strBuilder11.insert(2, new StringBuffer("234"), 0, 2));
        System.out.println("在strBuffer11偏移3位后插入Date的结果为:"
                + strBuffer11.insert(3, new StringBuilder("234"), 0, 2));

    }
}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • 删除某个或某些字符

删除某个或某些的字符方法如下图所示:

Java中字符串StringBuffer和StringBuilder的使用

相关示例代码如下:

public class StringTest {

    public static void main(String[] args) {

        StringBuilder strBuilder = new StringBuilder("hello");
        StringBuffer strBuffer = new StringBuffer("hello");
        System.out.println("strBuilder删除索引为1处的字符后结果为:" + strBuilder.deleteCharAt(1));
        System.out.println("strBuffer删除索引为1处的字符后结果为:" + strBuffer.deleteCharAt(1));

        StringBuilder strBuilder2 = new StringBuilder("hello");
        StringBuffer strBuffer2 = new StringBuffer("hello");
        System.out.println("strBuilder2删除索引从1到3(不包括3)的字符后结果为:" + strBuilder2.delete(1, 3));
        System.out.println("strBuffer2删除索引从1到3(不包括3)的字符后结果为:" + strBuffer2.delete(1, 3));

    }
}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • ​取子字符串

取子字符串的方法如下:

Java中字符串StringBuffer和StringBuilder的使用

示例代码如下所示:

public class StringTest {

    public static void main(String[] args) {

        StringBuilder strBuilder = new StringBuilder("hello world");
        StringBuffer strBuffer = new StringBuffer("hello world");
        System.out.println("strBuilder从索引3处开始到字符串末尾的子字符串为:" + strBuilder.substring(3));
        System.out.println("strBuffer从索引3处开始到字符串末尾的子字符串为:" + strBuffer.substring(3));

        StringBuilder strBuilder2 = new StringBuilder("hello world");
        StringBuffer strBuffer2 = new StringBuffer("hello world");
        System.out.println("strBuilder从索引3处开始到5(不包括5)的子字符串为:" + strBuilder.substring(3, 5));
        System.out.println("strBuffer从索引3处开始到5(不包括5)的子字符串为:" + strBuffer.substring(3, 5));

    }
}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用
  • 其他

除了以上列举的方法外,还有一些常用的方法,分别为:

  1. int capacity():获取容量(初始化字符数组的长度)
  2. int length():获取长度(实际字符的长度)
  3. replace(int start, int end, String str):将索引从start到end(不包括end)的字符序列替换为str
  4. char charAt(int index):获取指定索引出的字符
  5. void setCharAt(int index, char ch):用ch替换指定索引处的字符
  6. void setLength(int newwLength):将字符序列强制变为指定长度,多余的字符被置为null。
  7. CharSequence subSequence(int start, int end):获取子字符序列对象。
  8. reverse():将字符序列进行反转。

示例代码如下所示:

public class StringTest {

    public static void main(String[] args) {

        StringBuilder strBuilder = new StringBuilder("hello world");
        StringBuffer strBuffer = new StringBuffer("hello world");

        // 获取字符容量
        System.out.println("strBuilder的容量为:" + strBuilder.capacity());
        System.out.println("strBuffer的容量为:" + strBuffer.capacity());

        // 获取长度
        System.out.println("strBuilder的长度为:" + strBuilder.length());
        System.out.println("strBuffer的长度为:" + strBuffer.length());

        // 替换字符串
        System.out.println("用"tom"替换strBuilder的0到5(不包括5)后结果为:"
                + strBuilder.replace(0, 5, "tom"));
        System.out.println("用"tom"替换strBuffer的0到5(不包括5)后结果为:"
                + strBuffer.replace(0, 5, "tom"));

        // 获取指定索引处的字符
        System.out.println("strBuilder的索引为1的字符为:" + strBuilder.charAt(1));
        System.out.println("strBuffer的索引为1的字符为:" + strBuffer.charAt(1));

        // 将字符序列强制变为指定长度
        strBuilder.setLength(3);
        System.out.println("strBuilder强制变为长度为3的结果为:" + strBuilder);
        strBuffer.setLength(3);
        System.out.println("strBuffer强制变为长度为3的结果为:" + strBuffer);

        // 取子字符序列
        System.out.println("strBuilder的从0到2的子字符序列为:" + strBuilder.subSequence(0, 2));
        System.out.println("strBuffer的从0到2的子字符序列为:" + strBuffer.subSequence(0, 2));

        // 字符序列反转
        System.out.println("strBuilder的反转后为:" + strBuilder.reverse());
        System.out.println("strBuffer的反转为:" + strBuffer.reverse());
    }
}

执行结果如下图所示:

Java中字符串StringBuffer和StringBuilder的使用

​自此,关于StringBuffer和StringBuilder的介绍就已经算是结束了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论