【原】Java学习笔记023 - 字符串缓冲区_正则表达式

时间:2021-04-04 18:46:13
  1 package cn.temptation;
2
3 import java.util.Arrays;
4
5 public class Sample01 {
6 public static void main(String[] args) {
7 // 因为字符串创建后就不能修改,导致在进行字符串拼接时,会产生大量的中间字符串,创建对象都是需要消耗资源
8 // 所以,能不用字符串的直接拼接尽量不使用
9
10 // 字符串缓冲区:StringBuffer类/StringBuilder类
11 // 1、类 StringBuffer:线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。 (线程安全)
12 // 2、类 StringBuilder:一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。 (线程不安全)
13
14 // 感性认识:安全的东西常常意味着效率会较低;不安全的东西常常意味着效率会较高
15
16 // String 和 StringBuffer的区别:
17 // 1、String:值在创建之后不能更改,在字符串常量池中产生大量的对象,会消耗大量的资源,影响程序的效率
18 // 2、StringBuffer:长度和内容均可以改变,可以节省资源,提高效率
19
20 // StringBuffer:字符串缓冲区,有容量,自然理解为容器
21
22 // StringBuffer类的构造函数:
23 // 1、StringBuffer() :构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
24 // 2、StringBuffer(int capacity) :构造一个不带字符,但具有指定初始容量的字符串缓冲区。
25 // 3、StringBuffer(String str) :构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
26
27 // StringBuffer类的常用成员方法:
28 // 1、int capacity() :返回当前容量。
29 // 2、int length() :返回长度(字符数)。
30
31 StringBuffer sb1 = new StringBuffer();
32 System.out.println("sb1:" + sb1); // 无内容
33 System.out.println("sb1`s capacity:" + sb1.capacity()); // 16
34 System.out.println("sb1`s length:" + sb1.length()); // 0
35 System.out.println("------------------------------------------");
36
37 StringBuffer sb2 = new StringBuffer(10);
38 System.out.println("sb2:" + sb2); // 无内容
39 System.out.println("sb2`s capacity:" + sb2.capacity()); // 10
40 System.out.println("sb2`s length:" + sb2.length()); // 0
41 System.out.println("------------------------------------------");
42
43 StringBuffer sb3 = new StringBuffer("java");
44 System.out.println("sb3:" + sb3); // java
45 System.out.println("sb3`s capacity:" + sb3.capacity()); // 20
46 System.out.println("sb3`s length:" + sb3.length()); // 4
47 }
48 }
49
50 // 查看StringBuffer类的源码
51 //@Override
52 //public synchronized String toString() {
53 // if (toStringCache == null) {
54 // toStringCache = Arrays.copyOfRange(value, 0, count);
55 // }
56 // return new String(toStringCache, true);
57 //}
58 //
59 //public StringBuffer() {
60 // super(16);
61 //}
62 //
63 //public StringBuffer(int capacity) {
64 // super(capacity);
65 //}
66 //
67 //public StringBuffer(String str) {
68 // super(str.length() + 16);
69 // append(str);
70 //}
71 //@Override
72 //public synchronized int capacity() {
73 // return value.length;
74 //}
75 //
76 //@Override
77 //public synchronized int length() {
78 // return count;
79 //}
80 //@Override
81 //public synchronized StringBuffer append(String str) {
82 // toStringCache = null;
83 // super.append(str);
84 // return this;
85 //}
86 // 查看StringBuffer类的父类AbstractStringBuilder的源码
87 //char[] value;
88 //
89 //int count;
90 //
91 //AbstractStringBuilder(int capacity) {
92 // value = new char[capacity];
93 //}
94
95 //public int capacity() {
96 // return value.length;
97 //}
98
99 //@Override
100 //public int length() {
101 // return count;
102 //}
  1 package cn.temptation;
2
3 public class Sample02 {
4 public static void main(String[] args) {
5 /*
6 * StringBuffer类的常用成员方法:(具有增加功能)
7 * 1、StringBuffer append(String str) :将指定的字符串追加到此字符序列。
8 * 2、StringBuffer insert(int offset, String str) :将字符串插入此字符序列中。
9 */
10 StringBuffer sb = new StringBuffer();
11
12 System.out.println("sb:" + sb); // 无内容
13 System.out.println("sb.length():" + sb.length()); // 0
14
15 // 将指定的字符串追加到此字符序列
16 sb.append("java");
17
18 System.out.println("sb:" + sb); // java
19 System.out.println("sb.length():" + sb.length()); // 4
20
21 // 继续append
22 sb.append("temptation");
23
24 System.out.println("sb:" + sb); // javatemptation
25 System.out.println("sb.length():" + sb.length()); // 14
26
27 // 链式编程
28 sb.append("is").append("good");
29
30 System.out.println("sb:" + sb); // javatemptationisgood
31 System.out.println("sb.length():" + sb.length()); // 20
32
33 System.out.println("-----------------------------------------------");
34
35 StringBuffer sbTemp = new StringBuffer();
36 // 语法错误:The method append(Object) is ambiguous for the type StringBuffer
37 // sbTemp.append(null);
38
39 String strTemp = null;
40 sbTemp.append(strTemp);
41 System.out.println("sbTemp:" + sbTemp); // null
42 System.out.println("sbTemp.capacity():" + sbTemp.capacity()); // 16
43 System.out.println("sbTemp.length():" + sbTemp.length()); // 4
44
45 // 注意:直接把null值append给一个字符串缓冲区对象,会产生语法错误;
46 // null值赋值给字符串,字符串再append给一个字符串缓冲区对象,语法OK,会显示"null"字符串
47
48 System.out.println("-----------------------------------------------");
49
50 StringBuffer sb1 = new StringBuffer();
51 System.out.println("sb1`s capacity:" + sb1.capacity()); // 16
52 System.out.println("sb1`s length:" + sb1.length()); // 0
53
54 // 放入长度为30的字符串
55 // sb1.append("123456789012345678901234567890");
56 // System.out.println("sb1`s capacity:" + sb1.capacity()); // 34
57 // System.out.println("sb1`s length:" + sb1.length()); // 30
58
59 // 放入长度为40的字符串
60 sb1.append("1234567890123456789012345678901234567890");
61 System.out.println("sb1`s capacity:" + sb1.capacity()); // 40
62 System.out.println("sb1`s length:" + sb1.length()); // 40
63
64 System.out.println("-----------------------------------------------");
65
66 StringBuffer sb2 = new StringBuffer();
67 sb2.append("tom");
68 System.out.println("sb2:" + sb2); // sb2:tom
69 System.out.println("sb2`s capacity:" + sb2.capacity()); // 16
70 System.out.println("sb2`s length:" + sb2.length()); // 3
71
72 sb2.insert(1, "jerry");
73
74 System.out.println("sb2:" + sb2); // sb2:tjerryom
75 System.out.println("sb2`s capacity:" + sb2.capacity()); // 16
76 System.out.println("sb2`s length:" + sb2.length()); // 8
77
78 // 注意:理解append方法 和 insert方法,类似在食堂打菜,append方法是遵守秩序排队的,insert方法是插队的
79 }
80 }
81
82 // 查看StringBuffer类的源码
83 //@Override
84 //public synchronized StringBuffer append(String str) {
85 // toStringCache = null;
86 // super.append(str);
87 // return this;
88 //}
89 //
90 // 查看StringBuffer类的父类AbstractStringBuilder类的源码
91 //public AbstractStringBuilder append(String str) {
92 // if (str == null)
93 // return appendNull();
94 // int len = str.length(); // len:30 40
95 // ensureCapacityInternal(count + len); // count:0 0
96 // str.getChars(0, len, value, count);
97 // count += len;
98 // return this;
99 //}
100 //
101 //private void ensureCapacityInternal(int minimumCapacity) {
102 // // overflow-conscious code
103 // if (minimumCapacity - value.length > 0) { // minimumCapacity:30 value.length:16 minimumCapacity:40 value.length:16
104 // value = Arrays.copyOf(value,
105 // newCapacity(minimumCapacity));
106 // }
107 //}
108 //
109 // 创建StringBuffer对象的新容量方法
110 //private int newCapacity(int minCapacity) {
111 // // overflow-conscious code // minCapacity:30 minCapacity:40
112 // int newCapacity = (value.length << 1) + 2; // value.length << 1 相当于 value.length * 2 , newCapacity:34
113 // if (newCapacity - minCapacity < 0) { // 34 - 30 34 - 40
114 // newCapacity = minCapacity; // 30时不会走入选择结构 newCapacity:40
115 // }
116 // return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
117 // ? hugeCapacity(minCapacity)
118 // : newCapacity; // 返回newCapacity:34 返回newCapacity:40
119 //}
 1 package cn.temptation;
2
3 public class Sample03 {
4 public static void main(String[] args) {
5 /*
6 * StringBuffer类的常用成员方法:(具有删除功能)
7 * 1、StringBuffer deleteCharAt(int index) :移除此序列指定位置的 char。
8 * 2、StringBuffer delete(int start, int end) :移除此序列的子字符串中的字符。
9 */
10 StringBuffer sb = new StringBuffer();
11 sb.append("java");
12
13 System.out.println("sb:" + sb); // sb:java
14 System.out.println("sb.length():" + sb.length()); // 4
15
16 sb.deleteCharAt(2);
17
18 System.out.println("sb:" + sb); // sb:jaa
19 System.out.println("sb.length():" + sb.length()); // 3
20
21 System.out.println("----------------------------------------------------------");
22
23 sb.append("dota").append("lol");
24
25 System.out.println("sb:" + sb); // sb:jaadotalol
26 System.out.println("sb.length():" + sb.length()); // 10
27
28 sb.delete(1, 4);
29
30 System.out.println("sb:" + sb); // sb:jotalol
31 System.out.println("sb.length():" + sb.length()); // 7
32
33 // 移除StringBuffer对象的全部字符
34 // sb.delete(0, sb.length());
35 //
36 // System.out.println("sb:" + sb); // sb:
37 // System.out.println("sb.length():" + sb.length()); // 0
38
39 // 执行异常:String index out of range: -1
40 // sb.delete(-1, 2);
41
42 // 执行异常:java.lang.StringIndexOutOfBoundsException
43 // sb.delete(8, 10);
44
45 // 执行异常:java.lang.StringIndexOutOfBoundsException
46 // sb.delete(3, 2);
47 }
48 }
 1 package cn.temptation;
2
3 public class Sample04 {
4 public static void main(String[] args) {
5 /*
6 * StringBuffer类的常用成员方法:(具有替换功能)
7 * 1、StringBuffer replace(int start, int end, String str) :使用给定 String 中的字符替换此序列的子字符串中的字符。
8 */
9 StringBuffer sb = new StringBuffer();
10 sb.append("javatemptation");
11 System.out.println("sb:" + sb); // sb:javatemptation
12
13 // sb.replace(3, 7, "xyza");
14 // System.out.println("sb:" + sb); // sb:javxyzaptation
15
16 System.out.println("---------------------------------------------");
17
18 // 使用给定 String 中的字符替换此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,
19 // 如果不存在这种字符,则一直到序列尾部。
20 // 先将子字符串中的字符移除,然后将指定的 String 插入 start。(如果需要,序列将延长以适应指定的字符串。)
21
22 // start~end的范围小于str替换字符串的长度,在原字符串中,按start~end的范围把字符移除,再把替换字符串插入禁区,原字符串长度发生改变
23 sb.replace(3, 5, "xyza");
24 System.out.println("sb:" + sb); // sb:javxyzaemptation
25
26 System.out.println("---------------------------------------------");
27
28 // 使用给定 String 中的字符替换此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,
29 // 如果不存在这种字符,则一直到序列尾部。
30 // 先将子字符串中的字符移除,然后将指定的 String 插入 start。(如果需要,序列将延长以适应指定的字符串。)
31
32 // start~end的范围大于str替换字符的长度,在原字符串中,按start~end的范围把字符移除,再把替换字符串插入禁区,原字符串长度发生改变
33
34 sb.replace(3, 10, "xyza");
35 System.out.println("sb:" + sb); // sb:javxyzatation
36 }
37 }
 1 package cn.temptation;
2
3 public class Sample05 {
4 public static void main(String[] args) {
5 /*
6 * StringBuffer类的常用成员方法:(具有反转功能)
7 * 1、StringBuffer reverse() :将此字符序列用其反转形式取代。
8 */
9 String str = "abcd";
10 System.out.println(str);
11
12 char[] arrChar = str.toCharArray();
13 String result = "";
14 for (int i = arrChar.length - 1; i >= 0; i--) {
15 result += arrChar[i];
16 }
17 System.out.println(result);
18
19 System.out.println("-------------------------------------------");
20
21 StringBuffer sb = new StringBuffer();
22 sb.append("abcd");
23 System.out.println(sb);
24
25 sb.reverse();
26
27 System.out.println(sb);
28 }
29 }
 1 package cn.temptation;
2
3 public class Sample06 {
4 public static void main(String[] args) {
5 /*
6 * StringBuffer类的成员方法:(具有截取功能)
7 * 1、String substring(int start) :返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
8 * 2、String substring(int start, int end) :返回一个新的 String,它包含此序列当前所包含的字符子序列。
9 */
10 StringBuffer sb = new StringBuffer();
11
12 sb.append("javatemptation");
13 System.out.println("sb:" + sb); // sb:javatemptation
14
15 String str1 = sb.substring(3);
16 System.out.println("sb:" + sb); // sb:javatemptation
17 System.out.println("str1:" + str1); // str1:atemptation
18
19 System.out.println("-----------------------------------------");
20
21 // 没有截取
22 // String str2 = sb.substring(4, 4);
23
24 // 包左不包右
25 String str2 = sb.substring(4, 8); // temp
26
27 // 执行异常:String index out of range: -1
28 // String str2 = sb.substring(4, 3);
29
30 // 执行异常:String index out of range: -4
31 // String str2 = sb.substring(-4, 3);
32
33 // 执行异常:java.lang.StringIndexOutOfBoundsException: String index out of range: -7
34 // String str2 = sb.substring(4, -3);
35
36 // 执行异常:java.lang.StringIndexOutOfBoundsException: String index out of range: -12
37 // String str2 = sb.substring(15, 3);
38
39 // 执行异常:java.lang.StringIndexOutOfBoundsException: String index out of range: 15
40 // String str2 = sb.substring(5, 15);
41
42 System.out.println("sb:" + sb); // sb:javatemptation
43 System.out.println("str2:" + str2);
44 }
45 }
 1 package cn.temptation;
2
3 public class Sample07 {
4 public static void main(String[] args) {
5 // String对象 和 StringBuffer对象的互相转换
6
7 // String对象 -----> StringBuffer对象
8
9 // 方法1:使用StringBuffer类的构造函数
10 StringBuffer sb1 = new StringBuffer("abcd");
11 // 方法2:使用StringBuffer类的构造函数和append()方法
12 StringBuffer sb2 = new StringBuffer();
13 sb2.append("abcd");
14
15 System.out.println(sb1 == sb2); // false 比较的是这两个StringBuffer对象引用的地址
16 System.out.println(sb1.equals(sb2)); // false 追踪StringBuffer类的equals方法源码,发现其没有重写继承自Object类的equals方法
17
18 System.out.println("--------------------------------------------");
19
20 // StringBuffer对象 -----> String对象
21
22 // 方法1:使用String类的构造函数
23 String str1 = new String(sb1);
24 // 方法2:使用StringBuffer类的toString()方法
25 String str2 = sb1.toString();
26
27 System.out.println(str1 == str2); // false 比较的是这两个地址
28 System.out.println(str1.equals(str2)); // true String类重写了继承自Object类的equals方法
29 }
30 }
 1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample08 {
6 public static void main(String[] args) {
7 // 需求:分别使用String和StringBuffer实现如下功能
8 // 判断字符串是否为回文字符串(对称字符串)
9 // 例如:回文字符串:"lol"、"BKB"、"ABBA"
10 String str = "";
11
12 System.out.println("输入一个字符串:");
13 Scanner input = new Scanner(System.in);
14 if (input.hasNextLine()) {
15 str = input.nextLine();
16 } else {
17 System.out.println("输入不正确!");
18 }
19 input.close();
20
21 // 思路1:(使用String)
22 // 循环比较第1个字符和最后1个字符是否相同,再比较第2个字符和倒数第2个字符是否相同...
23
24 char[] arrChar = str.toCharArray();
25 boolean flag = true;
26
27 // 写法1-1
28 // for (int i = 0, j = arrChar.length - 1; i <= j ; i++, j--) {
29 // // 判断相同
30 // if (arrChar[i] != arrChar[j]) {
31 // flag = false;
32 // break;
33 // }
34 // }
35
36 // 写法1-2
37 for (int i = 0; i < arrChar.length / 2; i++) {
38 // 判断相同
39 if (arrChar[i] != arrChar[arrChar.length - i - 1]) {
40 flag = false;
41 break;
42 }
43 }
44
45 System.out.println(flag ? "字符串" + str + "是回文字符串" : "字符串" + str + "不是回文字符串");
46
47 // 思路2:(使用StringBuffer)
48 // 将字符串进行反转,把得到的字符串和原字符串进行比较,如果相同就是回文字符串
49
50 // 写法2-1
51 // StringBuffer sb = new StringBuffer(str);
52 //
53 // String result = sb.reverse().toString();
54 // System.out.println(result.equals(str) ? "字符串" + str + "是回文字符串" : "字符串" + str + "不是回文字符串");
55
56 // 写法2-2(链式编程)
57 System.out.println((new StringBuffer(str)).reverse().toString().equals(str)
58 ? "字符串" + str + "是回文字符串"
59 : "字符串" + str + "不是回文字符串");
60
61 // 注意:StringBuffer对象使用reverse()方法反转后,其自身内容发生了反转
62 }
63 }
 1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample09 {
6 public static void main(String[] args) {
7 // 需求:制作一个方法对QQ号码进行验证(验证规则:QQ长度从5位~11位,只能是数字,且首位不能是0)
8
9 // 思路:
10 // 1、接收键盘录入的字符串
11 // 2、字符串的规则的校验
12
13 String str = "";
14
15 System.out.println("输入一个QQ号码:");
16 Scanner input = new Scanner(System.in);
17 if (input.hasNextLine()) {
18 str = input.nextLine();
19 } else {
20 System.out.println("输入不正确!");
21 }
22 input.close();
23
24 String result = validateQQ(str) ? "是QQ号码" : "不是QQ号码";
25 System.out.println(result);
26
27 }
28
29 /**
30 * 验证QQ号码
31 * @param qq
32 * @return 验证结果
33 */
34 public static boolean validateQQ(String qq) {
35 boolean flag = true;
36
37 if (qq.length() >= 5 && qq.length() <= 11) {
38 if (qq.charAt(0) == '0') {
39 // 首位字符为'0'
40 flag = false;
41 } else {
42 // 各个位置上都是数字的判断
43 for (int i = 0; i < qq.length(); i++) {
44 if (!(qq.charAt(i) >= '0' && qq.charAt(i) <= '9')) {
45 flag = false;
46 break;
47 }
48 }
49 }
50 } else {
51 // 长度不在5位~11位这个范围
52 flag = false;
53 }
54
55 return flag;
56 }
57 }
 1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample10 {
6 public static void main(String[] args) {
7 // 因为字符串的相关操作在工作中特别普遍,所以出现一种高效但是抽象的字符串操作技巧(方法)----- 正则表达式
8
9 // 需求:制作一个方法对QQ号码进行验证(验证规则:QQ长度从5位~11位,只能是数字,且首位不能是0)
10
11 String str = "";
12
13 System.out.println("输入一个QQ号码:");
14 Scanner input = new Scanner(System.in);
15 if (input.hasNextLine()) {
16 str = input.nextLine();
17 } else {
18 System.out.println("输入不正确!");
19 }
20 input.close();
21
22 // 使用正则表达式
23 String regex = "[1-9][0-9]{4,10}";
24 boolean flag = str.matches(regex);
25
26 String result = flag ? "是QQ号码" : "不是QQ号码";
27 System.out.println(result);
28 }
29 }
 1 package cn.temptation;
2
3 public class Sample11 {
4 public static void main(String[] args) {
5 String str1 = "door";
6 String regex1 = "do*r"; // * 匹配前面的子表达式零次或多次
7 System.out.println(str1 + ":" + str1.matches(regex1)); // true
8
9 // String str1Ex = "oper"; // 匹配为false
10 // String str1Ex = "dr"; // 匹配为true
11 String str1Ex = "dooooor"; // 匹配为true
12 System.out.println(str1Ex + ":" + str1Ex.matches(regex1));
13
14 System.out.println("------------------------------------------------");
15
16 String str2 = "door";
17 String regex2 = "do+r"; // + 匹配前面的子表达式一次或多次
18 System.out.println(str2 + ":" + str2.matches(regex2)); // true
19
20 // String str2Ex = "dr"; // 匹配为false
21 String str2Ex = "dooooor"; // 匹配为true
22 System.out.println(str2Ex + ":" + str2Ex.matches(regex2));
23 System.out.println("------------------------------------------------");
24
25 String str3 = "door";
26 String regex3 = "do?r"; // ? 匹配前面的子表达式零次或一次
27 System.out.println(str3 + ":" + str3.matches(regex3)); // false
28
29 // String str3Ex = "dr"; // 匹配为true
30 String str3Ex = "dor"; // 匹配为true
31 System.out.println(str3Ex + ":" + str3Ex.matches(regex3));
32 System.out.println("------------------------------------------------");
33
34 String str4 = "banana";
35 String regex4 = "b[an]*a"; // [] 框起来的部分作为一个整体
36 System.out.println(str4 + ":" + str4.matches(regex4)); // true
37 System.out.println("------------------------------------------------");
38
39 String str5 = "door";
40 String regex5 = "do{2}r"; // {n} n 是一个非负整数,匹配确定的n次
41 System.out.println(str5 + ":" + str5.matches(regex5)); // true
42 System.out.println("------------------------------------------------");
43
44 String str6 = "doooooooor";
45 String regex6 = "do{2,}r"; // {n,} n 是一个非负整数,至少匹配n次
46 System.out.println(str6 + ":" + str6.matches(regex6)); // true
47 System.out.println("------------------------------------------------");
48
49 String str7 = "doooooooor";
50 String regex7 = "do{2,9}r"; // {n,m} n 和 m 均是一个非负整数,且n <= m,最少匹配n次且最多匹配m次
51 System.out.println(str7 + ":" + str7.matches(regex7)); // true
52 }
53 }