StringBuffe
- 获取int类型的最大值和最小值
System.out.println(Integer.MAX_VALUE);//int类型的最大值
System.out.println(Integer.MIN_VALUE);//int类型的最小值
输出结果
- Integer和String相互转换
Integer i1 = new Integer(100);
System.out.println(i1);
Integer i2 = new Integer("100");
System.out.println(i2);
输出结果
int和String相互转换
//int--String
int i = 100;
System.out.println(i + ""); //1、和""进行拼接
System.out.println(String.valueOf(i)); //2、String.valueOf()
Integer i3 = new Integer(i);
System.out.println(Integer.toString(i)); //3、Integer类的toString方法
System.out.println(i3); //4、Integer类的静态方法
//String--int
String str = "123";
Integer i4 = new Integer(str);
int is = i4.intValue();
System.out.println(is);
int is1 = Integer.parseInt(str);
System.out.println(is1);
System.out.println("----------------------");
//基本数据类型包装有八种,其中七种都有parseXxx方法,可以将这七种的字符串表现形式转换成基本数据类型
char[] c = str.toCharArray();
System.out.println(c);
输出结果
- StringBuffer
public class Demo13_StringBuffer {
public static void main(String[] agrs) {
StringBuffer sb = new StringBuffer();//空参构造
System.out.println(sb.length());//输出0;容器中字符的个数,实际值
System.out.println(sb.capacity());//输出16;容器的初始容量,理论值
StringBuffer sb1 = new StringBuffer(10);//指定容量的字符串缓冲区对象
System.out.println(sb.length());//输出0;
System.out.println(sb1.capacity());//输出10
StringBuffer sb2 = new StringBuffer("abcd");//指定字符串内容的字符串缓冲区对象
System.out.println(sb2.length());//输出4;
System.out.println(sb2.capacity());//输出20,字符串的length+容器的初始容量
}
}
输出结果
public class Demo13_StringBuffer1 {
public static void main(String[] agrs) {
StringBuffer sb = new StringBuffer();
//append可以把任意类型数据添加到字符串缓冲区里面
System.out.println(sb.append(123455));
System.out.println(sb.append("abcde"));
StringBuffer sb1 = new StringBuffer("123456");
//insert在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
System.out.println(sb1.insert(3, "abcd"));
StringBuffer sb2 = new StringBuffer("ABCDEF");
//deleteCharAt删除指定位置的字符,并返回本身
System.out.println(sb2.deleteCharAt(4));
//delete删除从指定位置开始指定位置结束的内容,并返回本身
System.out.println(sb2.delete(0, 3));
System.out.println(sb2.delete(0, sb1.length()));
//replace(int start,int end,String str),从start开始到end用str代替
StringBuffer sb3 = new StringBuffer("123456789");
System.out.println(sb3.replace(0,3,"abc"));
//reverse();字符串反转
StringBuffer sb4 = new StringBuffer("123456789");
System.out.println(sb4.reverse());
//substring(int start),从指定位置截取到末尾,返回值不再是StringBuffer本身,而是String
StringBuffer sb5 = new StringBuffer("123456789");
String sbstr =sb5.substring(6);
System.out.println(sbstr);
System.out.println(sb5);
//substring(int start,int end),截取从指定开始位置到指定结束位置,返回值不再是StringBuffer本身,而是String本身
StringBuffer sb6 = new StringBuffer("123456789");
String sbstr1 =sb6.substring(0,6);
System.out.println(sbstr1);
System.out.println(sb6);
}
}
输出结果
- StringBuffer和String的相互转换
public class Demo13_StringBuffer2 {
/**
* StringBuffer和String的相互转换
*/
public static void main(String[] agrs) {
//String转换为StringBuffer
StringBuffer sb = new StringBuffer("abc"); //通过构造方法将字符串转换为StringBuffer
System.out.println(sb);
StringBuffer sb1 = new StringBuffer(); //通过append() 将字符串转换为StringBuffer
System.out.println(sb1.append("123"));
//StringBuffer转换为String
StringBuffer sb2 = new StringBuffer("Abc");
String s = new String(sb2); //通过构造方法将StringBuffer转换为String
System.out.println(s);
System.out.println(sb2.toString());//通过toString方法将StringBuffer转换为String
System.out.println(sb2.substring(0,sb2.length()));//通过substring(int start,int end)方法将StringBuffer转换为String
}
}
输出结果
- String和StringBuffer分别作为参数传递
public class Demo13_StringBuffer3 {
public static void main(String[] agrs) {
//String作为参数传递,基本数据类型的值传递不改变其值
String s = "abc";
System.out.println(s);//输出abc
change(s);
System.out.println(s);//输出abc
String str1="021";
String str = str1+"123";
System.out.println(str1);
change(str);
System.out.println(str);
System.out.println("-----------------------------");
//StringBuffer作为参数传递,引用数据类型的值传递,改变其值
StringBuffer sb = new StringBuffer();
sb.append("ABCD");
System.out.println(sb);//输出ABCD
change(sb);
System.out.println(sb);//输出ABCDEFG
}
private static void change(StringBuffer sb) {
sb.append("EFG");
}
private static void change(String s) {
s +="123";
}
}
输出结果
- StringBuffer功能实现(测试)
public class Demo13_StringBufferTest {
public static void main(String[] agrs) {
int[]arr={1,2,3};
StringBuffer sb = new StringBuffer("["); //创建一个字符串缓冲区对象,将[添加到缓冲区
for (int i =0;i<arr.length;i++){ //遍历数组
if (i == arr.length-1){ //添加判断条件,如果数组索引是最后一位的话就添加],java中数组索引从0开始,所以需要-1
sb.append(arr[i]).append("]");
}else {
sb.append(arr[i]).append(",");
}
}
System.out.println(sb.toString());
}
}
输出结果
正则表达式
- 正则表达式(测试)
/**
* 需求:校验qq号码.
* * 1:要求必须是5-15位数字
* * 2:0不能开头
* * 3:必须都是数字
*/
String regex = "[1-9]\\d{4,14}";
System.out.println("012365".matches(regex));//false
System.out.println("12365".matches(regex));//true
System.out.println("dvsv12365".matches(regex));//false
System.out.println("653435434343543534512365".matches(regex));//false
输出结果
public static void main(String[] agrs) {
regex12();
regex34();
regex5678();
private static void regex12() {
String regex1 ="[abc]";
System.out.println("a".matches(regex1));//ture
System.out.println("g".matches(regex1));//false
System.out.println("---------------------------");
String regex2 ="[^abc]";
System.out.println("a".matches(regex2));//false
System.out.println("g".matches(regex2));//ture
System.out.println("---------------------------");
}
private static void regex34() {
String regex3 ="[[a-zA-Z]]";
System.out.println("a".matches(regex3));//ture
System.out.println("G".matches(regex3));//ture
System.out.println("2".matches(regex3));//false
System.out.println("---------------------------");
String regex4 ="[0-9]";
System.out.println("a".matches(regex4));//false
System.out.println("2".matches(regex4));//ture
}
private static void regex5678() {
String regex5 ="[a-d[m-p]]";
System.out.println("a".matches(regex5));//ture
System.out.println("n".matches(regex5));//ture
System.out.println("2".matches(regex5));//false
System.out.println("---------------------------");
String regex6 ="[a-z&&[def]]";
System.out.println("a".matches(regex6));//false
System.out.println("d".matches(regex6));//ture
System.out.println("---------------------------");
String regex7 ="[a-z&&[^bc]]";
System.out.println("a".matches(regex7));//ture
System.out.println("1".matches(regex7));//false
System.out.println("c".matches(regex7));//false
System.out.println("---------------------------");
String regex8 ="[a-z&&[^m-p]]";
System.out.println("a".matches(regex8));//ture
System.out.println("n".matches(regex8));//false
}
}
输出结果