黑马程序员——Java基础---常用类--Object类,Scanner类,String类,StringBuffer类,StringBuilder类

-----------android培训java培训、java学习型技术博客、期待与您交流!------------


一、Object

()概述:Object类是类层次结构的根类,所有类都直接或者间接的继承自该类。

()构造方法:public Object();

()Object类的成员方法

1. public int hashCode():返回对象的哈希码值,计算结果是一个十进制数

2. public String toString():返回字符串

3. public boolean equals(Object obj):返回布尔值,参数类:Object obj,是用来比较对象的。

4. protected void finalize():垃圾回收器,对象变成垃圾后,垃圾回收器调用方法finalize,垃圾收取,自动方式,不需要开发人员去处理。

5. protected Object clone():克隆对象

   6. public void wait():导致当前线程等待

   7. public void notify():唤醒等待中的线程

   8. public void nofifyAll():唤醒全部等待中的线程

9. public Class getClass():返回类的class文件对象

(1)toString()方法案例演示

package object;

public class ObjectToString {

public static void main(String[] args) {

Person p =new Person("zz",24);

System.out.println(p);

//Person类继承Object,可以直接使用父类方法toString()

System.out.println(p.toString());

}

}

class Person extends Object{

private String name;

private int age;

public Person() {

super();

}

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String toString(){

return name+"--"+age;

}

}

运行结果:

 

(2) equals(Object obj)方法案例演示

package object;

public class ObjectEquals {

public static void main(String[] args) {

//建立2Student类的对象

StudentDemo s1=new StudentDemo(15);

StudentDemo s2=new StudentDemo(18);

//调用方法equals进行比较

boolean b=s1.equals(s2);

System.out.println(b);

}

}

class StudentDemo{

private int age;

public StudentDemo() {}

public StudentDemo(int age) {

super();

this.age = age;

}

//重写equals方法,建立Student类自己的比较方式

//标胶两个对象中成员变量age的值,如果一样返回true

public boolean equals(Object obj){

//判断obj是不是null,如果是,就不需要比较

if (obj==null) {

return false;

}

//判断thisobj是不是同一个对象,如果是就不需要比较

if (this==obj) {

return true;

}

if (obj instanceof StudentDemo) {//判断,传递的参数obj是不是Student类型

//如果是,可以强转,不是Student类型,不要比了

StudentDemo s=(StudentDemo)obj;

return this.age==s.age;

}

return false;

}

}

运行结果:

 

(3)细节

①clone,finalize方法权限受保护

Object,所有类的父类,为什么方法权限受保护

受保护权限:对外中的子类,可以调用,必须持有super才能调用

父类构造保护的方法,只能是子类方法中super才能调用

② == 和equals区别

==

对于基本类型,比较两边具体数值

对于引用类型,比较的是对象的内存地址

equals 默认比较也是对象内存地址

重写equals建立自己的对象的比较方式

二、Scanner

()Scanner类概述

JDK5以后用于获取用户的键盘输入

()构造方法

public Scanner(InputStream source)

()基本格式

1. hasNextXxx():判断是否还有下一个输入项,其中Xxx可以是IntDouble等。如果需要判断是否包含下一个字符串,则可以省略Xxx

2. nextXxx():获取下一个输入项。Xxx的含义和上个方法中的Xxx相同

3. 默认情况下,Scanner使用空格,回车等作为分隔符

()常用方法

public int nextInt():接收键盘输入整数

public String nextLine():接收键盘输入字符串

案例演示:

package scanner;

import java.util.Scanner;

public class ScannerDemo {

public static void main(String[] args) {

method();

}

//接收键盘输入

public static void method(){

Scanner sc = new Scanner(System.in);

System.out.println("请输入整数。。。");

//接收整数

int x = sc.nextInt();//123 回车键 \r\n

System.out.println(x);

sc = new Scanner(System.in);

System.out.println("请输入一行");

//接收键盘一行

String line = sc.nextLine();//接收 \r\n

System.out.println(line);

}

}

运行结果:

 

三、String

()String类概述

String是字符串的类类型,用于描述字符串事物。字符串是一个特殊的对象。

字符串是由多个字符组成的一串数据(字符序列)

字符串可以看成是字符数组

()构造方法

1. public String(byte[] bytes):传递字节数组,将参数字节数组变成字符串

2. public String(byte[] bytes,int offset,int length):传递字节数组,两个整数,将字节数组中的一部分变成字符串

3. public String(char[] value):传递字符数组,将字符数组变成字符串

4. public String(char[] value,int offset,int count):传递字符数组,将字符数组一部分转成字符串,第一个参数表示开始索引,第二个参数表示获取几个

案例演示:

package string;

/*

 * String类的构造方法

 */

public class StringDemo {

public static void main(String[] args) {

method1();//将字节数组变成一个字符串

method2();//将字节数组一部分,变成字符串

method3();//将字符数组变成字符串

method4();//将字符数组一部分,变成字符串

}

/*

 * 构造方法,传递字符数组,2个整数

 * String(char[] ch,int offset,int length)

 * 将字符数组一部分,变成字符串

 * offset 开始索引

 * length 获取几个

 * 不会查询编码表

 */

public static void method4(){

//定义字符数组

char[] ch = {'a','b','c','d','e','f','g'};

//String构造方法,传递字符数组,2个整数

String s = new String(ch,3,3);

System.out.println(s);

}

/*

 * 构造方法,传递字符数组

 * String(char[] ch)

 * 将字符数组变成字符串

 * 不会查询编码表

 */

public static void method3(){

//定义字符数组

char[] ch = {'a','b','c','d','e','f','g'};

//String构造方法,传递字符数组

String s = new String(ch);

System.out.println(s);

}

/*

 * 构造方法,传递字节数组,2个整数

 * String(byte[] bytes,int offset,int length)

 * 将字节数组一部分,变成字符串

 * offset开始索引

 * length 获取几个

 */

public static void method2(){

//定义字节数组

byte[] bytes = {97,98,99,100,101};

//String类构造方法,传递字节数组,2个整数

String s = new String(bytes,2,3);

System.out.println(s);

}

/*

 * 构造方法,传递字节数组

 * String(byte[] bytes)字节数组变成一个字符串

 * new String的时候,传递字节数组

 * 查询操作系统中的编码表  GBK(简体中文)

 */

public static void method1(){

//定义字节数组

byte[] bytes = {97,98,99,100,101};

//字节数组传递String类的构造方法

String s = new String(bytes);

System.out.println(s);

}

}

运行结果:

 

()字符串的特点

1. 字符串本身是一个常量,一旦初始化,就不会被改变

2. JAVA语言中,只要写个双引号 "" 字符串对象,就是String类的对象

3. ""可以直接调用String类的方法,格式: "".String类的方法

4. 字符串的底层实现原理,字符串其实看成是一个字符数组:char[],格式:private final char value[]

5. String类,字符串表示,用的是字符数组:例如:"abc"相当于新建了一个数组char[] value = {'a','b','c'};数组建立好了,变成最终数组,长度就定了,内容还是里面的内容,不可改变。

案例演示:证明字符串的不变性

package string;

public class StringInvariant {

public static void main(String[] args) {

String s="lmn";

System.out.println(s);

method(s);

System.out.println(s);

}

public static void method(String s){

s=s+"hij";

}

}

运行结果:

 

()String类的判断方法--计算结果都是真假 boolean

1. public boolean equals(Object obj)

String类继承Object,重写equals方法,建立字符串自己的比较方式

比较的不是字符串的内存,比较的是实际的字符的具体内容,判断是不是完全相等,如果全等返回true

2. public boolean equalsIgnoreCase(String s)

比较两个字符串是否相等,忽略大小写

3. public boolean contains(String s)

判断一个字符串中,是否包含另一个字符串,全包含返回true

4. public boolean startsWith(String s)

判断一个字符串,是否以另一个字符串开头,是开头返回true

5. public boolean endsWith(String s)

判断一个字符串,是否以另一个字符串结尾,是结尾返回true

6. public boolean isEmpty()

判断一个字符串中是不是空的,如果一个字符串中一个字符都没有,返回true,注意:空字符串:""

案例演示:

package string;

public class StringJudge {

public static void main(String[] args) {

method1();//String类判断两个字符串是否全等

method2();//String类判断两个字符串是否相等,忽略大小写

method3();//判断一个字符串是否包含另一个字符串

method4();//判断一个字符串是否以另一个字符串开头

method5();//判断一个字符串是否以另一个字符串结尾

method6();//判断一个字符串中是否有字符

}

/*

 * String类判断一个字符串中是否有字符

 * isEmpty

 */

public static void method6(){

String s="";

//s1对象调用String类的方法isEmpty判断字符串中是否有字符

boolean b=s.isEmpty();

System.out.println("method6--"+b);

}

/*

 * String类判断一个字符串是否以另一个字符串结尾

 * endsWith("")

 */

public static void method5(){

String s1="HelloWolrd.java";

String s2=".java";

//s1对象调用String类的方法endsWith传递s2

//区分大小写

boolean b=s1.endsWith(s2);

System.out.println("method5--"+b);

}

/*

 * String类判断一个字符串是否以另一个字符串开头

 * startsWith("")

 */

public static void method4(){

String s1="HelloWolrd.java";

String s2="he";

//s1对象调用String类的方法startsWith传递s2

//区分大小写

boolean b=s1.startsWith(s2);

System.out.println("method4--"+b);

}

/*

 * 判断一个字符串是否包含另一个字符串

 * contains("");

 */

public static void method3(){

String s1="Hello World";

String s2=" Wo";

//s1对象调用String类的方法contains传递s2

//判断字符串s1中是否完全包含s2

boolean b=s1.contains(s2);

System.out.println("method3--"+b);

}

/*

 * String类判断两个字符串是否相等

 * 忽略大小写

 * equalsIgnoreCase("")

 * 应用:验证码

 */

public static void method2(){

String s1="no";

String s2="No";

//s1对象调用String类的方法equalsIgnoreCase,传递s2

//判断s1s2是否相等,忽略大小写

boolean b=s1.equalsIgnoreCase(s2);

System.out.println("method2--"+b);

}

/*

 * 判断两个字符串是否全等

 * equals("")

 * 应用:密码

 */

public static void method1(){

String s1="abc";

String s2="abc";

//s1对象调用String类的方法equals,传递s2

//判断字符串s1s2是不是全等

boolean b=s1.equals(s2);

System.out.println("method1--"+b);

}

}

运行结果:

 

()String类的获取方法

1. public char charAt(int index)

获取字符串中,指定的索引上的单个字符  "abcd"  返回指定索引上的单个字符

2. public int indexOf(char ch)

获取指定的字符,在字符串中第一次出现的索引,如果找不到字符,返回-1

3. public int indexOf(char ch, int fromIndex)

获取指定的字符,在字符串中第一次出现的索引,指定开始查找的索引,如果找不到字符,返回-1

4. public int indexOf(String s)

获取指定的字符串,在另一个字符串中第一次出现的索引,如果找不到字符串,返回-1

5. public int indexOf(String s, int fromIndex)

获取指定的字符串,在另一个字符串中第一次出现的索引,指定开始查找的索引,如果找不到字符串,返回-1

6. public int lastIndexOf(char ch)

获取指定的字符,在字符串中最后一次出现的索引,反向查找,如果找不到返回-1

7. public int lastIndexOf(char ch,int formIndex)

获取指定的字符,在字符串中最后一次出现的索引,反向查找,指定开始查找的索引,如果找不到返回-1

8. public int length()

获取字符串的长度,返回的是字符串中字符的个数

数组的属性:数组名.length

字符串的方法:字符串.length()

9. public String substring(int begin,int end)

获取字符串的一部分,开始索引,结束索引,返回新的字符串

案例演示:

package string;

public class StringHuoQu {

public static void main(String[] args) {

method();//获取指定索引上的字符

method1();//获取字符在字符串中第一次出现的索引

method2();//获取字符在字符串中第一次出现的索引,指定开始查找的索引

method3();//获取字符串在字符串中第一次出现的索引

method4();//获取字符串在字符串中第一次出现的索引,指定开始查找的索引

method5();//获取字符在字符串中最后一次出现的索引

method6();//获取字符在字符串中最后一次出现的索引,指定开始查找的索引

method7();//获取字符串长度

method8();//获取字符串的一部分

}

/*

 * string类获取字符串的一部分,开始索引,结束索引,返回新的字符串

 * 截取字符串:substring(int again,int end);

 * 包含头,不包含尾

 */

public static void method8(){

String s="abcdefghijklmnopqrstuvwxyz";

//s类对象调用String类的方法substring截取一部分字符串

s=s.substring(0, 2);

System.out.println("method8--"+s);

String s1="abcdefghijklmn";

//从开始索引,后面全要

s1=s1.substring(2);

System.out.println("method8--"+s1);

}

/*

 * String类获取字符串中字符个数

 * 字符串长度:length()

 */

public static void method7(){

String s="abcdefghijklmnopqrstuvwxyz";

//s对象调用String类的方法length获取字符串长度

int length=s.length();

System.out.println("method7--"+length);

}

/*

 * 反向查找

 * String类获取一个字符在字符串中从指定索引开始最后一次出现的索引

 * 指定开始查找的索引

 * lastIndexOf(char ch,int fromIndex)传递字符,传递开始查找的索引

 * 没有这个字符,返回-1(负数不能是索引)

 * 从开始索引往前找

 */

public static void method6(){

String s="abmkmlmdmugm";

//s对象调用String类的方法获取一个字符在字符串中最后一次出现的索引,传递字符,传递开始查找的索引

int index=s.lastIndexOf('m', 10);//7往前找,6

System.out.println("method6--"+index);

}

/*

 * 反向查找

 * String类获取一个字符在字符串中最后一次出现的索引

 * lastIndexOf(char ch)传递字符

 * 没有这个字符,返回-1(负数不能是索引)

 * 从后往前找

 */

public static void method5(){

String s="abmkmlmdmugm";

//s对象调用stirng类获取一个字符在字符串中最后一次出现的索引,传递字符

int index=s.lastIndexOf('m');

System.out.println("method5--"+index);

}

/*

 * String类获取一个字符串在字符串中第一次出现的索引,指定开始查找的索引

 * indexOf(String s,int fromIndex)传递字符,传递开始查找的索引

 * 没有这个字符,返回-1(负数不能是索引)

 * index为字符串被找到后的第一个字符所对应的索引值

 */

public static void method4(){

String s="very thank you";

//s对象调用String类的方法indexOf,传递字符串,传递指定索引,返回字符串第一次出现的索引

int index=s.indexOf("tha", 3);

System.out.println("method4--"+index);

}

/*

 * String类获取一个字符串在字符串中第一次出现的索引

 * indexOf(String s)传递字符

 * 没有这个字符,返回-1(负数不能是索引)

 * index为字符串被找到后的第一个字符所对应的索引值

 */

public static void method3(){

String s="very thank you";

//s对象调用String类的方法indexOf传递字符串,返回字符串第一次出现时第一个字符的索引

int index=s.indexOf("tha");

System.out.println("method3--"+index);

}

/*

 * String类获取字符在字符串中第一次出现的索引,指定开始查找的索引

 * indexOf(char ch,int fromIndex)传递字符,传递开始查找的索引

 * 没有这个字符,返回-1(负数不能是索引)

 */

public static void method2(){

String s="very thank you";

//s对象调用String类的方法indexOf,传递字符,传递开始查找索引,返回查到时字符第一次出现的索引

int index=s.indexOf('v', 3);

System.out.println("method2--"+index);

}

/*

 * String类获取字符在字符串中第一次出现的索引

 * indexOf(char ch)传递字符,返回第一次出现的索引

 * 没有这个字符,返回-1

 */

public static void method1(){

String s="very thank you";

//s对象调用String类的方法indexOf传递字符,返回第一次出现时的索引

int index=s.indexOf('r');

System.out.println("method1--"+index);

}

/*

 * String类获取指定索引上的单个字符

 * charAT(int index),传递索引,返回字符

 * 索引没有,出现字符串越界异常:StringIndexOutofBoundsException

 */

public static void method(){

String s="what your name?";

//s对象调用String类的方法charAt,传递索引,返回单个字符

char ch=s.charAt(6);

System.out.println("method--"+ch);

}

}

运行结果:

 

() String类转换方法

1. public byte[]  getBytes()

将字符串转成字节数组,查询本机默认编码表  

2. public char[]  toCharArray()

将字符串转成字符数组,不查询编码表 

3. public String toUpperCase()

将字符串全部转成大写字母  

4. public String toLowerCase()

将字符串全部转成小写字母  

5. public String concat(String s)

将两个字符串连接起来  +

6. public static String valueOf(传递任意数据类型)

将任意数据类型变成字符串

案例演示:

package string;

public class StringTransform {

public static void main(String[] args) {

transform();//String 类中字符数组与字符串的相互转换

transform1();//String 类中字符数组与字符串的相互转换

transform2();//String类中将字符串转成全部大写和全部小写

transform3();//String 类将两个字符串连接

transform4();//String 类将任意数据类型变成字符串

}

/*

 * String 类将任意数据类型变成字符串

 * static valueOf(写任意数据类型)

 */

public static void transform4(){

//s对象调用String类的方法valueOf,实现将任意数据类型转变成字符串

String s=String.valueOf(false);

System.out.println("transform4--"+s);

}

/*

 * String 类将两个字符串连接

 * concat(String s)

 */

public static void transform3(){

//s对象调用String类的方法concat,实现字符串的连接

String s="mnl".concat("hi");

System.out.println("transform3--"+s);

}

/*

 * String类中将字符串转成全部大写和全部小写

 * toUpperCase

 * toLowerCase

 */

public static void transform2() {

String s="ABHItmn";

//s对象调用String类的方法toUpperCase

String upper=s.toUpperCase();

System.out.println("transform2--"+upper);

//s对象调用String类的方法toLowerCase

String lower=s.toLowerCase();

System.out.println("transform2--"+lower);

}

/*

 * String类中字符串与字符数组的相互转换,不查询编码表

 */

public static void transform1(){

String s="ef";

//s对象调用String类方法toCharArray,将字符串转成字符数组

char[] ch=s.toCharArray();

//遍历数组

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

for (int i = 0; i < ch.length; i++) {

System.out.println(ch[i]);

}

char[] ch2={'a','d'};

String s2=new String(ch2);

System.out.println("transform1--"+s2);

}

/*

 * String类中字节数组与字符串的相互转换,需查询编码表

 */

public static void transform(){

//""可以直接调用String类的方法

String s="kl";

//字符串转成字节数组

byte[] bytes=s.getBytes();

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

for (int i = 0; i < bytes.length; i++) {

System.out.println(bytes[i]);

}

//字节数组转成字符串

byte[] bytes2={65,66};

String s2=new String(bytes2);

System.out.println("transform--"+s2);

}

}

运行结果:

 

()String的其他方法

1. public String[] split(String s)

切割字符串,传递参数就是一个切割的标准

2. public String replace(char oldChar,char newChar)

替换字符串,被替换以前的,替换以后的新字符

3. public String replace(String oldString,String newString)

替换字符串,被替换以前的字符串,替换以后的新字符串

4. public String trim()

去掉字符串两端空格,返回新的字符串,中间空格不理会

5. public int compareTo(String s)

两个字符串进行比较,称为字符串的自然顺序,字典顺序abcd  ABCD

按照字符串的字典顺序进行比较

返回值是整数,比较后出现三种结果  s1.compareTo(s2)

s1小于s2  < 0 负数

s1等于s2  = 0

s1大于s2  > 0 正数

案例演示:

package string;

public class ElseStringFunction {

public static void main(String[] args) {

method1();//String 类切割字符串,返回字符数组

method2();//String类替换字符方法,返回新的字符串

method3();//String类替换字符串方法,返回新的字符串

method4();//String 类字符串字典顺序(自然顺序)比较

method5();//String 类去掉字符串两端的空格

}

/*

 * String 类去掉字符串两端空格

 * trim()返回新字符串

 * 注册,登录使用

 */

public static void method5(){

String s=" mm nn ";

System.out.println("去两端空格前--"+s);

//s对象调用String类方法trim

s=s.trim();

System.out.println("去两端空格后--"+s);

}

/*

 * String类字符串字典顺序比较

 * 字符的自然顺序

 * compareTo("")

 * 调用和传递的参数在比较

 * 调用者大于参数,返回正数

 * 调用者小于参数,返回负数

 * 调用者等于参数,返回0

 */

public static void method4(){

String s1="asc";

String s2="azz";

//String s2=new String("azz");同上一样

//s1对象调用String类的方法compareTo传递s2

int n=s1.compareTo(s2);

System.out.println("methed4--"+n);

}

/*

 * String类替换字符串方法

 * replace传递老的字符串,传递新的字符串

 * 返回新的字符串

 */

public static void method3(){

String s="www.itcast.cn";

//s对象调用String类的方法replace传递老字符串,新字符串

s=s.replace("itcast","CSDN");

System.out.println("method3--"+s);

}

/*

 * String类替换字符方法,被替换以前的字符,替换以后的新字符

 * replace(char oldChar,char newChar)

 * 返回新的字符串

 */

public static void method2(){

String s="www.itcast.cn";

//s对象调用String类的方法replace,实现替换字符,返回新字符串

s=s.replace('w''t');

System.out.println("method2--"+s);

}

/*

 * string类切割字符串

 * split(切割标准)返回新的字符串数组

 * 区分大小写

 */

public static void method1(){

System.out.println("method1");

String s="192.168.123.45";

//s对象调用String类方法split切割字符串

String[] str=s.split("\\.");//转义字符\\

//遍历字符串数组

for (int i = 0; i < str.length; i++) {

System.out.println(str[i]);

}

}

}

运行结果:

 

()String类练习

1. 字符串反转举例:键盘录入”abc” 输出结果:”cba”

package string;

import java.util.Scanner;

public class StringFanZhuan {

public static void main(String[] args) {

fanZhuan();

}

public static void fanZhuan(){

//键盘录入

Scanner sc=new Scanner(System.in);

System.out.println("请从键盘输入字符串:");

String s=sc.nextLine();

//字符串转成字符数组

char[] ch=s.toCharArray();

//对字符数组进行反转

for (int i = 0,j=ch.length-1; i < j; i++,j--) {

char t=ch[i];

ch[i]=ch[j];

ch[j]=t;

}

//字符数组转成字符串

String s1=new String(ch);

System.out.println("反转后的字符串为:");

System.out.println(s1);

}

}

运行结果:

 

2. 统计大串中小串出现的次数

举例:在字符串” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次

package string;

public class TestGetCount {

public static void main(String[] args) {

tongJi();

}

public static void tongJi(){

String big="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";

String small="java";

//定义变量,记录indexOf的结果

int index=0;

//定义计数器

int count=0;

while ((index=big.indexOf(small,index))!=-1) {

//计数器++

count++;

//获取找到后,寻找下一个开始的索引

index=index+small.length();

}

System.out.println("java出现了"+count+"");

}

}

运行结果:

 

四、StringBuffer

()概述:线程安全的可变字符序列,StringBuffer是字符缓冲区,用于存储字符串数据,所以也称之为容器。

()构造方法

public StringBuffer() 

public StringBuffer(int capacity)

public StringBuffer(String str)

()StringBuffer类的成员方法

添加功能

public StringBuffer append(String str)

public StringBuffer insert(int offset,String str)

删除功能

public StringBuffer deleteCharAt(int index)

public StringBuffer delete(int start,int end)

替换功能

public StringBuffer replace(int start,int end,String str)

反转功能

public StringBuffer reverse()

截取功能

public String substring(int start)

public String substring(int start,int end)

截取功能和前面几个功能的不同

返回值类型是String类型,本身没有发生改变

1. 向缓冲区追加数据案例演示

package string_buffer;

public class Append {

public static void main(String[] args) {

/*

 * 向缓冲区追加数据

 * append(任意类型)

 * 方法的返回值是 StringBuffer类型

 * 方法是StringBuffer类的对象

 * 谁调用我,我返回谁

 */

//创建StringBuffer对象

StringBuffer buffer=new StringBuffer();

//buffer对象调用StringBuffer类方法append,追加数据

buffer.append("zz").append(false).append(1.1).append('a');

System.out.println(buffer);

}

}

运行结果:

 

2. StringBuffer类的删除方法的案例演示

package string_buffer;

public class Delete {

public static void main(String[] args) {

method();//删除指定索引上的单个字符

method1();//删除指定索引内的字符

}

/*

 * StringBuffer 类的删除方法

 * deleteCharAt(int index);删除指定索引上的单个字符

 */

public static void method(){

//创建缓冲区对象

StringBuffer buffer =new StringBuffer("mnhijk");

//buffer 对象调用StringBuffer类方法delete,删除指定索引位置上的单个字符

buffer.deleteCharAt(3);

System.out.println(buffer);

}

/*

 * StringBuffer类的删除方法

 * delete(int begin,int end)删除指定索引内的字符

 * Java中,凡是开始结尾下标,都是包含头,不包含尾

 */

public static void method1(){

//创建缓冲区对象

StringBuffer buffer =new StringBuffer("abcdef89");

//buffer对象调用StringBuffer类方法delete,删除指定索引范围内的字符

buffer.delete(3, 6);

System.out.println(buffer);

}

}

运行结果:

 

3. StringBuffer类的添加方法的案例演示

package string_buffer;

public class Insert {

public static void main(String[] args) {

/*

 * StringBuffer类的添加功能

 * 数据添加到字符串缓冲区中

 * insert(索引,要添加的数据)

 * 在缓冲区的指定索引位置上插入数据

 */

//创建缓冲区对象

StringBuffer buffer=new StringBuffer("abcdefghijk");

//buffer对象调用StringBuffer方法Insert在指定索引位置上插入数据

buffer.insert(7, 8);

System.out.println(buffer);

}

}

运行结果:

 

4. StringBuffer类的反转方法的案例演示

package string_buffer;

public class Reverse {

public static void main(String[] args) {

reverse();

}

public static void reverse(){

//创建StringBuffer对象,使用空参构造方法

StringBuffer buffer=new StringBuffer();

//buffer对象调用StringBuffer类方法append,追加数据

buffer.append("phone");

System.out.println("反转缓冲区前:"+buffer);

//buffer对象调用StringBuffer类的方法reverse,反转缓冲区

buffer.reverse();

System.out.println("反转缓冲区后:"+buffer);

}

}

运行结果:

 

5. StringBuffer类的替换方法的案例演示

package string_buffer;

public class ReplaceDemo {

public static void main(String[] args) {

method();

}

/*

 * 替换缓冲区中指定的字符

 * 开始和结束的索引

 * replace(int start,int end,String str)

 */

private static void method() {

StringBuffer buffer = new StringBuffer();

buffer.append("abcdef");

//替换缓冲区 1-3索引,替换为itcast

buffer.replace(1, 3, "itcast");

System.out.println(buffer);

}

}

运行结果:

 

6. StringBuffer类的截取方法案例演示

package string_buffer;

public class JieQu {

public static void main(String[] args) {

StringBuffer buffer=new StringBuffer();

//向缓冲区追加数据

buffer.append("HelloWorld");

//buffer对象调用substring方法,截取字符串,传递开始索引,结束索引,返回字符串

//包含头,不包含尾

String s=buffer.substring(0, 5);

System.out.println(s);

}

}

运行结果:

 

7. StringStringBuffer的相互转换

不变对象,转成可变对象:StringàStringBuffer

StringBuffer类的方法toString()

可变对象,转成不可变对象:StringBufferàString

StringBuffer类构造方法

StringBuffer类的方法append

案例演示:

package string_buffer;

public class ZhuanHuan {

public static void main(String[] args) {

method();

method1();

}

//String-->StringBuffer

public static void method(){

StringBuffer buffer=new StringBuffer("abcd");

buffer.append("efj");

System.out.println(buffer);

}

//StringBuffer-->String

public static void method1(){

StringBuffer buffer = new StringBuffer();

buffer.append("HelloWorld");

String s=buffer.toString();

System.out.println(s);

}

}

运行结果:

 

8. 把数组拼接成一个字符串

package string_buffer;

public class PingJie {

public static void main(String[] args) {

int[] arr={1,2,3,4};

String s=method(arr);

System.out.println(s);

}

public static String method(int[] arr){

StringBuffer buffer=new StringBuffer();

buffer.append("[");

for (int i = 0; i < arr.length; i++) {

if (i!=arr.length-1) {

buffer.append(arr[i]+",");

}

else buffer.append(arr[i]+"]");

}

return buffer.toString();

}

}

运行结果:

 

9. 把字符串反转

package string_buffer;

public class Reverse1 {

public static void main(String[] args) {

//创建StringBuffer对象

StringBuffer buffer=new StringBuffer();

buffer.append("HelloWorld");

fanzhuan(buffer);

System.out.println("反转后:"+buffer);

}

public static void fanzhuan(StringBuffer buffer){

System.out.println("反转前:"+buffer);

//将缓冲区变成字符串,再变成字符数组

char[] ch=buffer.toString().toCharArray();

for (int start=0,end=ch.length-1; start < end; start++,end--) {

char t=ch[start];

ch[start]=ch[end];

ch[end]=t;

}

//清空缓冲区

buffer.delete(0, buffer.length());

//数组变成缓冲区

buffer.append(ch);

}

}

运行结果:

 

五、StringBuilder

StringBuilder类方法与StringBuffer完全一样

StringBufferJDK1.0出现

StringBuilderJDK1.5出现

StringBuilderStringBuffer的区别

StringBuffer线程安全,速度慢

   StringBuilder线程不安全,速度快

-----------android培训java培训、java学习型技术博客、期待与您交流!------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值