首页 > 作文

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

更新时间:2023-04-05 14:25:15 阅读: 评论:0

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

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

构造器

stringbuffer和stringbuilder的构造器都有三个,分别如下:

stringbuffer的构造器:

new stringbuffer(int capacity)new stringbuffer(string str)new stringbuffer(charquence q)

stringbuilder的构造器:

new stringbuilder(int capacity)new stringbuilder(string str)new stringbuilder(charquence q)

对比两者构造器发现,它们对构造器的重载都施行了同样的方式,可以传入一个整数的容量,可以传一个字符串值,甚至可以传一个字符序列的对象。对于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());    }}

执行结果如下图所示:

拼接参数

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

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

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

只想结果如下图所示:

获取某个字符串在另一个字符串中的索引位置

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

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

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    昵称qq    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));    }}

执行结果如下图所示:

插入方法

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

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

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.inrt(2, 3));        system.out.println("在strbuffer偏移3位后插入整数3的结果为:" + strbuffer.inrt(3, 3));               stringbuilder strbuilder2 = new stringbuilder("world");        stringbuffer strbuffer2 = new stringbuffer("world");        system.out.println("在strbuilder2偏移2位后插入'好'的结果为:" + strbuilder2.inrt(2, '好'));        system.out.println("在strbuffer2偏移3位后插入'好'的结果为:" + strbuffer2.inrt(3, '好'));        stringbuilder strbuilder3 = new stringbuilder("what");        stringbuffer strbuffer3 = new stringbuffer("what");        system.out.println("在strbuilder3偏移2位后插入4l的结果为:" + strbuilder3.inrt(2, 4l));        system.out.println("在strbuffer3偏移3位后插入4l的结果为:" + strbuffer3.inrt(3, 4l));        stringbuilder strbuilder4 = new stringbuilder("where");        stringbuffer strbuffer4 = new stringbuffer("where");        system.out.println("在strbuilder4偏移2位后插入3.14f的结果为:" + strbuilder4.inrt(2, 3.14f));        system.out.println("在strbuffer4偏移3位后插入3.14f的结果为:" + strbuffer4.inrt(3, 3.14f));        stringbuilder strbuilder5 = new stringbuilder("when");        stringbuffer strbuffer5 = new stringbuffer("when");        system.out.println("在strbuilder5偏移2位后插入1.414的结果为:" + strbuilder5.inrt(2, 1.414));        system.out.println("在strbuffer5偏移3位后插入1.414的结果为:" + strbuffer5.inrt(3, 1.414));        stringbuilder strbuilder6 = new stringbuilder("crazy");        stringbuffer strbuffer6 = new stringbuffer("crazy");        system.out.println("在strbuilder6偏移2位后插入true的结果为:" + strbuilder6.inrt(2, true));        system.out.println("在strbuffer6偏移3位后插入fal的结果为:" + strbuffer6.inrt(3, fal));        stringbuilder strbuilder7 = new stringbuilder("hehe");        stringbuffer strbuffer7 = new stringbuffer("hehe");        system.out.println("在strbuilder7偏移2位后插入date的结果为:" + strbuilder7.inrt(2, new date()));        system.out.println("在strbuffer7偏移3位后插入date的结果为:" + strbuffer7.inrt(3, new date()));        stringbuilder strbuilder8 = new stringbuilder("this");        stringbuffer strbuffer8 = new stringbuffer("this");        system.out.println("在strbuilder8偏移2位后插入字符数组['a', 'r', 'e']的结果为:"                + strbuilder8.inrt(2, new char[]{'a', 'r', 'e'}));        system.out.println("在strbuffer8偏移3位后插入字符数组['a', 'r', 'e']的结果为:"                + strbuffer8.inrt(3, new char[]{'a', 'r', 'e'}));        stringbuilder strbuilder9 = new stringbuilder("happend");        stringbuffer strbuffer9 = new stringbuffer("happend");        system.out.println("在strbuilder9偏移2位后插入date的结果为:"                + strbuilder9.inrt(2, new stringbuffer("234")));        system.out.println("在strbuffer9偏移3位后插入date的结果为:"                + strbuffer9.inrt(3, new stringbuilder("234")));        stringbuilder strbuilder10 = new stringbuilder("that");        stringbuffer strbuffer10 = new stringbuffer("that");        system.out.println("在strbuilder10偏移2位后插入字符数组['a', 'r', 'e']的结果为:"                + strbuilder10.inrt(2, new char[]{'a', 'r', 'e'}, 0, 2));        system.out.println("在strbuffer10偏移3位后插入字符数组['a', 'r', 'e']的结果为:"                + strbuffer10.inrt(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.inrt(2, new stringbuffer("234"), 0, 2));        system.out.println("在strbuffer11偏移3位后插入date的结果为:"                + strbuffer11.inrt(3, new stringbuilder("234"), 0, 2));    }}

执行结果如下图所示:

删除某个或某些字符

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

相关示例代码如下:

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

执行结果如下图所示:

​取子字符串

取子字符串的方法如下:

示例代码如下所示:

public class stringtest {    public static void main(string[] a作文素材摘抄rgs) {        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));    }}

执行结果如下图所示:

其他

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

int capacity():获取容量(初始化字符数组的长度)int length():获取长度(实际字符的长度)replace(int start, int end, string str):将索引从start到end(不包括end)的字符序列替换为strchar charat(int index):获取指定索引出的字符void tcharat(int index, char ch):用ch替换指定索引处的字符void tlength(int newwlength):将字符序列强制变为指定长度,多余的字符被置为null。charquence subquence(int start, int end):获取子字符序列对象。rever():将字符序列进行反转。

示例代码如下所示:

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.tlength(3);        system.out.println("strbuilder强制变为长度为3的结果为:" + strbuilder);        strbuffer.tlength(3);        system.out.println("strbuffer强制变为长度为3的结果为:" + strbuff狗感冒吃什么药er);        // 取子字符序列        system.out.println("strbuilder的从0到2的子字符序列为:" + strbuilder.subquence(0, 2));        system.out.println("strbuffer的从0到2的子字符序列为:" + strbuffer.subquence(0, 2));        // 字符序列反转        system.out.println("strbuilder的反转后为:" + strbuilder.rever());        system.out.println("strbuffer的反转为:" + strbuffer.rever());    }}

执行结果如下图所示:

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

本文发布于:2023-04-05 14:25:13,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/81791b18c443b01dfd9a05975a0b31aa.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:stringbuffer截取字符串的下标(解析springmvc工作流程).doc

本文 PDF 下载地址:stringbuffer截取字符串的下标(解析springmvc工作流程).pdf

标签:字符   序列   所示   字符串
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图