欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-24 21:13:10
java中表示字符串的有三个类:string、stringbuffer和stringbuilder。其中,string的长度是不可变的,而stringbuffer和stringbuilder是长度可变...

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());
    }
}

执行结果如下图所示:

stringbuffer截取字符串的下标(解析springmvc工作流程)
  • 拼接参数

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

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

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

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

只想结果如下图所示:

stringbuffer截取字符串的下标(解析springmvc工作流程)
  • 获取某个字符串在另一个字符串中的索引位置

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

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

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

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

    }


}

执行结果如下图所示:

stringbuffer截取字符串的下标(解析springmvc工作流程)
  • 插入方法

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

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

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

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

    }
}

执行结果如下图所示:

stringbuffer截取字符串的下标(解析springmvc工作流程)
  • 删除某个或某些字符

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

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

相关示例代码如下:

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

    }
}

执行结果如下图所示:

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

取子字符串的方法如下:

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

示例代码如下所示:

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

    }
}

执行结果如下图所示:

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

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

  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());
    }
}

执行结果如下图所示:

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

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