前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java的StringBuffer可变字符串的追加及修改、查找

java的StringBuffer可变字符串的追加及修改、查找

作者头像
用户7886150
修改2020-12-11 10:37:00
9560
修改2020-12-11 10:37:00
举报
文章被收录于专栏:bit哲学院

参考链接: 如何在Java中初始化和比较字符串

-----------java的StringBuffer可变字符串的追加及修改--------------

构造方法摘要StringBuffer()            构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。StringBuffer(CharSequence seq)            public java.lang.StringBuilder(CharSequence seq) 构造一个字符串缓冲区,它包含与指定的 CharSequence 相同的字符。StringBuffer(int capacity)            构造一个不带字符,但具有指定初始容量的字符串缓冲区。StringBuffer(String str)            构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

例子:

publicclass Test2 {

   publicstaticvoidmain(String[] args) {

      //创建可变字符串,开辟了一个默认是长度为0,容量为16个字符的空间

     StringBuffer sb = newStringBuffer();

     System.out.println("输出可变字符串的长度:" + sb.length()+ "输出可变字符串的容量:" + sb.capacity());

      //追加字符串

      sb.append("hellohellohellohello");

     System.out.println("输出可变字符串的长度:" + sb.length()+"输出可变字符串的容量:" + sb.capacity());

      //创建一个10个字符容量的可变字符串

     StringBuffer sb1 = newStringBuffer(10);

      sb1.append("hellohellohellohello");

     System.out.println("输出可变字符串的长度:" + sb.length()+"输出可变字符串的容量:" + sb.capacity());

      //创建一个带有字符串的参数的可变字符串对象

     StringBuffer sb2 = newStringBuffer("hellohellohellohello");

     System.out.println("输出可变字符串的长度:" + sb.length()+"输出可变字符串的容量:" + sb.capacity());

   }

}

结果:

输出可变字符串的长度:0输出可变字符串的容量:16

输出可变字符串的长度:20输出可变字符串的容量:34

输出可变字符串的长度:20输出可变字符串的容量:34

输出可变字符串的长度:20输出可变字符串的容量:34

---------------StringBuffer的追加方法-----------------------------------

StringBufferappend(boolean b)            Appends the string representation of the boolean argument to the sequence. StringBufferappend(char c)            Appends the string representation of the char argument to this sequence. StringBufferappend(char[] str)            Appends the string representation of the char array argument to this sequence. StringBufferappend(char[] str, int offset, int len)            Appends the string representation of a subarray of the char array argument to this sequence. StringBufferappend(CharSequence s)            将指定的 CharSequence 添加到该序列。 StringBufferappend(CharSequence s, int start, int end)            Appends a subsequence of the specified CharSequence to this sequence. StringBufferappend(double d)            Appends the string representation of the double argument to this sequence. StringBufferappend(float f)            Appends the string representation of the float argument to this sequence. StringBufferappend(int i)            Appends the string representation of the int argument to this sequence. StringBufferappend(long lng)            Appends the string representation of the long argument to this sequence. StringBufferappend(Object obj)            Appends the string representation of the Object argument. StringBufferappend(String str)            Appends the specified string to this character sequence. StringBufferappend(StringBuffer sb)            将指定的 StringBuffer 添加到此序列中。

例子:

/**

 * StringBuffer中的追加方法测试

 * @作者李昆鹏

 *

 */

publicclass Test2 {

   publicstaticvoidmain(String[] args) {

      //创建可变字符串,开辟了一个默认是长度为0,容量为16个字符的空间

     StringBuffer sb = newStringBuffer();

      sb.append(true)

       .append('a')

       .append(newchar[] {'a','g','g'})

       .append("hello")

       .append(100d)

       .append(14.5f)

       .append(13)

       .append(9001);

     System.out.println(sb);

     Object p = new Object();

      //追加了一个Object类的对象,返回的是对象的toString方法

      sb.append(p);

     System.out.println(sb);

     StringBuffer sb1 = newStringBuffer("helloworld");

      //可变字符串1追加可变字符串2

      sb.append(sb1);

     System.out.println(sb);

   }

}

结果:

trueaagghello100.014.5139001

trueaagghello100.014.5139001java.lang.Object@70dea4e

trueaagghello100.014.5139001java.lang.Object@70dea4ehelloworld

----------------------可变字符串的修改----------------------

插入方法:

 StringBufferinsert(int offset, boolean b)            Inserts the string representation of the boolean argument into this sequence. StringBufferinsert(int offset, char c)            Inserts the string representation of the char argument into this sequence. StringBufferinsert(int offset, char[] str)            Inserts the string representation of the char array argument into this sequence. StringBufferinsert(int index, char[] str, int offset, int len)            Inserts the string representation of a subarray of the str array argument into this sequence. StringBufferinsert(int dstOffset, CharSequence s)            Inserts the specified CharSequence into this sequence. StringBufferinsert(int dstOffset, CharSequence s, int start, int end)            Inserts a subsequence of the specified CharSequence into this sequence. StringBufferinsert(int offset, double d)            Inserts the string representation of the double argument into this sequence. StringBufferinsert(int offset, float f)            Inserts the string representation of the float argument into this sequence. StringBufferinsert(int offset, int i)            Inserts the string representation of the second int argument into this sequence. StringBufferinsert(int offset, long l)            Inserts the string representation of the long argument into this sequence. StringBufferinsert(int offset, Object obj)            Inserts the string representation of the Object argument into this character sequence. StringBufferinsert(int offset, String str)            Inserts the string into this character sequence.

例子:

/**

 * StringBuffer中的插入方法测试

 * @作者李昆鹏

 *

 */

publicclass Test2 {

   publicstaticvoidmain(String[] args) {

      //创建可变字符串,开辟了一个默认是长度为0,容量为16个字符的空间

     StringBuffer sb = newStringBuffer("helloworld");

      //在指定的索引位置插入字符串,注意不要出现字符串的索引越界问题

      sb.insert(2,true)

       .insert(10, "xxx")

       .insert(3, newchar[] {'a','b','c'});

       System.out.println(sb);

        char[] cs = {'a','b','c','d'};

        //第一个参数要插入的索引位置

        //第二个参数要插入的字符数组

        //第三个参数是数组的起始索引

        //第四个参数是要插入的长度(在插入的数组中从第三个参数忘后算几个,包含第三个参数)

        sb.insert(sb.length(),cs, 1, 2);

       System.out.println(sb);

   }

}

结果:

hetabcruellowxxxorld

hetabcruellowxxxorldbc

-----------------------可变字符串的删除------------------------

删除方法:

StringBufferdelete(int start, int end)            Removes the characters in a substring of this sequence. StringBufferdeleteCharAt(int index)            Removes the char at the specified position in this sequence.

例子:

/**

 * StringBuffer中的删除方法测试

 * @作者李昆鹏

 *

 */

publicclass Test2 {

   publicstaticvoidmain(String[] args) {

        //创建可变字符串,开辟了一个默认是长度为0,容量为16个字符的空间

       StringBuffer sb = newStringBuffer("helloworld");

        //第一个参数删除的开始索引,第二个参数是删除结束索引

        //删除的包含开始索引,不包含结束索引,结束索引不存在越界

        //如果是清空的话就是delete(0,sb.length()),

        sb.delete(2,4);

       System.out.println(sb);

        //删除指定索引的字符,很少用

        sb.deleteCharAt(1);

       System.out.println(sb);

   }

}

结果:

heoworld

howorld

---------------可变字符串的处理-------------------------

·替换

StringBufferreplace(int start, int end, String str)            Replaces the characters in a substring of this sequence with characters in the specified String

·字符串的反转

StringBufferreverse()            Causes this character sequence to be replaced by the reverse of the sequence.

·某个字符串的倒序索引

 intlastIndexOf(String str)            Returns the index within this string of the rightmost occurrence of the specified substring. intlastIndexOf(String str, int fromIndex)            Returns the index within this string of the last occurrence of the specified substring.

例子:

publicclass Test6 {

   publicstaticvoidmain(String[] args) {

      //创建可变字符串,开辟了一个默认是长度为0,容量为16个字符的空间

     StringBuffer sb = newStringBuffer("helloworld");

      //第一个参数是替换的开始索引,第二个参数是替换结束索引

      //第三个参数是替换的字符串

      sb.replace(2,5, "xx");

      System.out.println(sb);

      //把字符串倒过来,字符串的反转

      sb.reverse();

     System.out.println(sb);

      //获得倒数第一次出现的字符串

     StringBuffer sb1 = newStringBuffer("helloworld");

      intlastIndex = sb1.lastIndexOf("o");

     System.out.println(lastIndex);

      //获得从指定的索引开始向前数,找到倒数第一次出现的字符串的索引

      lastIndex = sb1.lastIndexOf("o", 5);

     System.out.println(lastIndex);

   }

}

结果:

hexxworld

dlrowxxeh

6

4

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档