您的位置:首页 > Web前端

StringBuffer类和类中的构造方法及其获取功能,添加功能,替换功能,添加功能,删除功能,反转功能,截取功能;

2018-07-28 16:12 337 查看

StringBuffer:线程安全的可变字符序列;

 

         StringBuffer的构造方法:
                          public StringBuffer()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。 
                          public StringBuffer(int capacity):指定容量大小
                          public StringBuffer(String str):构造一个字符串缓冲区:里面的容量(字符串内容英文)大小:字符串长度+16初始容量

一:获取功能:

                public int capacity():初始容量
                int length():返回字符串长度

    StringBuffer的构造方法和获取功能的举例:

[code]package org.westos.Demo2;

/*  StringBuffer的构造方法:
           public StringBuffer()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。 
           public StringBuffer(int capacity):指定容量大小
           public StringBuffer(String str):构造一个字符串缓冲区:里面的容量(字符串内容英文)大小:字符串长度+16初始容量

获取功能:
public int capacity():初始容量
           int length():返回字符串长度
* **/
public class StringBufferDemo {
public static void main(String[] args) {
// public StringBuffer()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符
StringBuffer sb = new StringBuffer();
System.out.println(sb);
System.out.println("capacity()=" + sb.capacity());// 获取容量
System.out.println("length()=" + sb.length());// 获取长度

System.out.println("----------------------------");

// public StringBuffer(int capacity):指定容量大小

StringBuffer sb1 = new StringBuffer(20);
System.out.println(sb1);
System.out.println("capacity()=" + sb1.capacity());// 获取容量
System.out.println("length()=" + sb1.length());// 获取长度

System.out.println("-----------------------------");
// public StringBuffer(String str):构造一个字符串缓冲区:里面的容量(字符串内容英文)大小:
// 字符串长度+16初始容量

StringBuffer sb2 = new StringBuffer("java");
System.out.println(sb2);
System.out.println("capacity()=" + sb2.capacity());// 获取容量
System.out.println("length()=" + sb2.length());// 获取长度
}

}

二:添加功能

    StringBuffer的添加功能:
                   StringBuffer append(xxx x):将指定的内容追加(末尾追加)到字符串缓冲区中,返回StringBuffer本身
                   public StringBuffer insert(int index,xxx x):在指定位置处,插入一个新的内容,返回StringBuffer本身

添加功能的举例:

[code]package org.westos.Demo2;

/*StringBuffer的添加功能:
*   StringBuffer append(xxx x):将指定的内容追加(末尾追加)到字符串缓冲区中,返回StringBuffer本身
*
*   public StringBuffer insert(int index,xxx x):在指定位置处,插入一个新的内容,
*   返回StringBuffer本身
*
**/
public class StringBufferDemo2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
// StringBuffer append(xxx x):将指定的内容追加(末尾追加)到字符串缓冲区中,返回StringBuffer本身
// sb.append("java");
// sb.append('S');
// sb.append(false);
// sb.append(100);
// sb.append(999.2);
// System.out.println(sb);

// 链式编程
sb.append("java").append('S').append(true).append(100).append(99.9);
System.out.println(sb);

// public StringBuffer insert(int index,xxx x):在指定位置处,插入一个新的内容,
// 返回StringBuffer本身

sb.insert(5, "love");
System.out.println(sb);

}
}

三:删除功能:  

             StringBuffer deleteCharAt(int index)  :删除指定位置出的字符,返回字符串缓冲区本身
             StringBuffer delete(int start, int end) :删除从指定位置开始到指定位置结束,返回值字符串缓冲区本身

StringBuffer删除功能举例:

四:替换功能:
                public StringBuffer replace(int start,int end,String str)
                从指定位置开始到指定位置结束,用给定的str字符串替换指定的字符内容

StringBuffer中转换功能的举例:

[code]package org.westos.Demo2;
/*    StringBuffer的替换功能:
* 			public StringBuffer replace(int start,int end,String str)
* 			从指定位置开始到指定位置结束,用给定的str字符串替换指定的字符内容
*
* **/

public class StringBufferDemo4 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("HelloWorld");

// public StringBuffer replace(int start,int end,String str)
// 从指定位置开始到指定位置结束,用给定的str字符串替换指定的字符内容
sb.replace(1, 3, "000000");
System.out.println(sb);

}

}

五:反转功能:

public StringBuffer reverse():将此字符序列用其反转形式取代

StringBuffer反转功能的举例:

[code]package org.westos.Demo2;

/*反转功能:
*         public StringBuffer reverse()
*         将此字符序列用其反转形式取代
* **/
public class StringBufferDemo5 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javase");
// public StringBuffer reverse():将此字符序列用其反转形式取代

sb.reverse();
System.out.println(sb);

}
}

六:截取功能:

             String substring(int start)  :从指定位置默认截取到末尾,返回一个新的字符串
             String substring(int start,int end):从指定位置开始截取到指定位置结束

StringBuffer截取功能的举例:

[code]package org.westos.Demo2;
/*   StringBuffer的截取功能:
* 	      String substring(int start)  :从指定位置默认截取到末尾,返回一个新的字符串
* 	      String substring(int start,int end):从指定位置开始截取到指定位置结束
* **/

public class StringBufferDemo6 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javasehelloworld");
// String substring(int start) :从指定位置默认截取到末尾,返回一个新的字符串

System.out.println(sb.substring(5));

// String substring(int start,int end):从指定位置开始截取到指定位置结束

System.out.println(sb.substring(3, 6));
}

}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐