StringBuffer常用类
package zz.itheima.stringbuffer;
public class TestStringBuffer {
//StringBuffer连接字符串的是偶不能用“+”,只能种append。
public static void main(String[] args) {
String s1="asdf";
//StringBuffer s2="asdf"; //没有这种用法
// public StringBuffer(),无参构造方法,构造一个空的字符串缓冲区,初始容量为16个字符
StringBuffer s3=new StringBuffer();
System.out.println(s3);
// public StringBuffer(int capacity),构造一个指定容量的空的字符串缓冲区
StringBuffer s4=new StringBuffer(20);
System.out.println(s4);
// public StringBuffer(String str),使用一个字符串作为初始内容来构造一个字符串缓冲区,并在后面预留16个字符的空缓冲区
StringBuffer s5=new StringBuffer("hello");
System.out.println(s5);
//System.out.println(s4+s5);+ 只能用于String类,不能用于StringBuffer的连接
// public StringBuffer append(任意类型 o),把任意类型数据的字符串表达形式追加到缓冲区的最后
// (例如:如果是对象,追加的是其toString方法的返回值)
s5.append(",world");
s5.append(100);
s5.append(new Book("java"));//添加的是一个对象,追加的是其toString方法的返回值
System.out.println(s5);
// public StringBuffer insert(int offset,任意类型 o),
// 把任意类型的字符串表达形式插入到缓冲区指定位置
s4.append("hello");
s4.insert(2, "kkk");
System.out.println(s4);
// public int capacity(),获得字符串缓冲区的当前容量
System.out.println(s4.capacity());
// public int length(),获得字符串缓冲区内字符串的长度
System.out.println(s4.length());
//容量如何增长,每次增长多少
s4.append(s4);
System.out.println(s4.capacity());
s4.append(s4);
s4.append("abc");
System.out.println(s4.capacity());
}
}
运行结果:
hello
hello,world100Book [name=java]
hekkkllo
20
8
20
42
字符串分割:
package zz.itheima.stringbuffer;
public class TestSplit {
public static void main(String[] args) {
//练习:字符串"aa.bb.cc"
String val="aa.bb.cc";
String[] temp=val.split("\\.");
System.out.println(temp.length);
//练习:字符串“av er t u op”
String str="av er t u op";
temp=str.split("\\s+");
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
//复杂的例子,叠词分割: "sdqqfgkkkhjppppkl" sd fg hj kl
String die_regex="(.)\\1{1,}"; //.{2,}
String s="sdqqfgkkkhjppppkl11";
temp=s.split(die_regex);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
s=s.replaceAll(die_regex, "$1"); //$表示从组中取一个值,1表示组的编号
System.out.println(s);
}
}
运行结果:
3
av
er
t
u
op
sd
fg
hj
kl
sdqfgkhjpkl1
正则表达式:
package zz.itheima.stringbuffer;
import java.util.regex.Pattern;
public class TestRegex {
//正则表达式
public static void main(String[] args) {
/*示例:郑州市固定电话(0371-56061160-223)
1.区号固定为0371
2.电话号码固定8位,不能以0开头
3.分机号可有可无,1-3位
4.都是数字
5.中间用“-”分割*/
//正则表达式常用于复杂格式(规则)的数据校验,管不了内容的正确性
//0371-[1-9]\\d{7}(-\\d{1,3})?
String phone_regex="0371-[1-9]\\d{7}(-\\d{1,3})?";
String phone="0371-960660-223";
System.out.println(phone.matches(phone_regex));
System.out.println(Pattern.matches(phone_regex, phone));
// 练习1:电子邮件
String email_regex="\\w+@[a-z0-9A-Z]+\\.[a-z]{2,}";
String email="ssdf@163.com";
System.out.println(email.matches(email_regex));
System.out.println(Pattern.matches(email_regex, email));
// 练习2:日期(yyyy-mm-dd) 2014-01-10 2014-10-10
String date_regex="[1-2]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])";
String date="2014-12-10";
System.out.println(date.matches(date_regex));
System.out.println(Pattern.matches(date_regex, date));
// 练习3:身份证号(18位 X)
String id_regex="\\d{17}[X|0-9]";
String id="410182198210254912";
System.out.println(id.matches(id_regex));
System.out.println(Pattern.matches(id_regex, id));
}
}
运行结果:
false
false
true
true
true
true
true
true
常用类:
package zz.itheima.stringbuffer;
public class TestMethod {
/**
* StringBuffer的一些常用类
* @param args
*/
public static void main(String[] args) {
// 测试StringBuffer类的常用方法
// public StringBuffer delete(int start,int end),删除缓冲区指定起始位置的字符串
StringBuffer s1=new StringBuffer("hello");
s1.delete(2, 4); //管前不管好
System.out.println(s1);
// public StringBuffer deleteCharAt(int index),删除缓冲指定位置的字符
s1.deleteCharAt(0);
System.out.println(s1);
// public StringBuffer replace(int start,int end,String str),
// 把缓冲区指定位置的字符串替换为新的字符串
StringBuffer s2=new StringBuffer("hello,world");
s2.replace(6, s2.length(), "java");
System.out.println(s2);
// public StringBuffer reverse(),字符串反转(倒)
s2.reverse();
System.out.println(s2);
//public String toString(),把StringBuffer转换为String类型
String ss=new StringBuffer("hello").toString()+new StringBuffer(",world").toString();
System.out.println(ss);
}
}
运行结果:
heo
eo
hello,java
avaj,olleh
hello,world
String类对象的不可变性和StringBuffer的可变性:
package zz.itheima.stringbuffer;
public class TestHashCode {
public static void main(String[] args) {
//String类对象的不可变性和StringBuffer的可变性。
String s1=new String("hello");
System.out.println(s1.hashCode());
s1=s1+",world";
System.out.println(s1.hashCode());//重新又指向了一个新的地址
StringBuffer s2=new StringBuffer("hello");
System.out.println(s2.hashCode());
s2.append(",world");
System.out.println(s2.hashCode());//还是原来的地址
}
}
运行结果:
99162322
2137655864
121075473
121075473
包装类的自动拆箱和装箱:
package zz.itheima.stringbuffer;
public class TestBaoZhuang {
//自动拆箱和装箱
public static void main(String[] args) {
int j=10;
Integer k=new Integer(10); //包装类 装箱
Integer h=10; //自动装箱
int f=k.intValue()+1; //拆箱
f=k+1; //自动拆箱
Byte b=new Byte((byte)2); //查帮助文档
//包装类转换为字符串
String s1=k+"";
s1=k.toString();
System.out.println(s1);
//字符串(兼容)转换为包装类
int result=Integer.valueOf(s1)+1;
System.out.println(result);
//基本数据类型转换为字符串
int n1=100;
String ss1=n1+"";
ss1=String.valueOf(n1);
//字符串(兼容)转为基本数据类型
String sk1="100";
int i1=Integer.parseInt(sk1);
String sk2="100.10";
double d1=Double.parseDouble(sk2);
}
}
运行结果:
10
11
分割例子:
package zz.itheima.stringbuffer;
public class Demo2 {
public static void main(String[] args) {
String s="5,3";
String[] temp=s.split(",");
int n1=Integer.parseInt(temp[0]);
int n2=Integer.parseInt(temp[1]);
System.out.println(n1+n2);
}
}
运行结果:
8
判断什么时候结束以及字符个数:
package zz.itheima.stringbuffer;
import java.util.Scanner;
public class Demo1 {
/**
* 让用户不断在控制台随意输入字符串,直到输入一个“q”或“Q”为止。
然后输出一共有几个i和o
最后把所有的i和o都删掉
请使用StringBuffer结合do-while实现上述功能
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
String val;
do {
//1.输出提示信息
System.out.println("请输入任意一个字符串:");
//2.录入
val = sc.nextLine();
//3.把录入的对象加入到StringBuffer中
sb.append(val);
} while (!val.equalsIgnoreCase("q"));
System.out.println(sb);
//然后计算一共录入几个I和O
//把StringBuffer转化为String
val = sb.toString();
char[] temp = val.toCharArray();
int n1 = 0;
int n2 = 0;
for (int i = 0; i < temp.length; i++) {
if(temp[i]=='i')
n1++;
if(temp[i]=='o')
n2++;
}
System.out.println("i有"+n1+"个,o有"+n2+"个");
// 最后把所有的i和o都删掉
val.replace("i", "").replace("o", "");
System.out.println("**********************");
}
}
运行结果:
请输入任意一个字符串:
dfjlkejriotweoio
请输入任意一个字符串:
q
dfjlkejriotweoioq
i有2个,o有3个
**********************
数据类:
package zz.itheima.stringbuffer;
public class Book {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book [name=" + name + "]";
}
public Book(String name) {
super();
this.name = name;
}
public Book() {
super();
}
}
09-10
09-10