Java中Stringbuilder和正則表達式示例詳解
StringBuilder
String 類型的連接性能不好,StringBuilder可以解決字符串連接性能問題。
在String
類型中,字符串是不可變的,每次連接字符串時都會創(chuàng)建一個新的String
對象,這會導致頻繁的內存分配和拷貝操作,影響性能。而StringBuilder
是可變的,可以在原有的字符串上進行操作,避免了頻繁的內存分配和拷貝,提高了連接字符串的性能。因此,使用StringBuilder
可以更有效地處理大量的字符串連接操作。
String s1 = "ABC"; String s2 = "def"; String s3 = s1 + s2;
字符串連接性能測試:
String str = ""; long t1 = System.currentTimeMillis(); for(int i=0; i<50000; i++){ str = str + "A"; } long t2 = System.currentTimeMillis(); System.out.pritnln(t2 - t1);
StringBuilder 用于提升String字符串的連接性
- - StringBuilder稱為可變字符串
- - StringBuilder內部也是字符數組, 其API可以直接修改其內部數組的內容
- - 當數組容量不足時候, 會自動擴容
- - 運算期間會盡力減少創(chuàng)建數組的數量。
package string; public class StringBuilderDemo03 { public static void main(String[] args) { /* * 測試StringBuilder的連接性能 */ StringBuilder buf = new StringBuilder(); long t1 = System.currentTimeMillis(); for (int i =0; i<50000; i++){ buf.append("A"); } long t2 = System.currentTimeMillis(); System.out.println(t2-t1); } }
//StringBuilder API buf-> char[]{A, C, B, 0, 0, 0, 0, 0, 0, 0, 0...0} // 0 1 2 3 4 5 StringBuilder buf = new StringBuilder(); buf.append("A") .append("A") .append("A") .append("B") .insert(1,"C") .delete(2,4); Stirng str = buf.toString();
- - append() 追加, 在StringBuilder的后面添加字符,當容量滿了,會自動擴容, 擴容規(guī)則 1倍+2;
- - insert(位置,字符) 插入字符;
- - delete(開始位置, 結束位置): 刪除一定范圍的字符,包括開始,不包括結束
- - StringBuilder的API返回的大多是當前對象,可以連續(xù)使用.調用方法。
- - toString() 方法可以講StringBuilder轉換為String
正則表達式
用于檢測、測試字符串規(guī)則的表達式.
經常用于檢測字符串是否符合特定的規(guī)則,在網站上經常用于檢測用戶輸入數據是否符合規(guī)范:
- - 檢測 用戶名 是否為 8~10 數字 英文(大小寫)
- - 檢測 電話號碼是否符合規(guī)則
- - 檢測 郵箱地址是否符合規(guī)則
正則HelloWorld
最簡單的正則表達式:"HelloWorld" 表示
- 一共有10個字符
- 出現的順序必須是 HelloWorld
- 用于檢測一個字符串是否符合,正則規(guī)則
- boolean matchs(正則) 檢測當前字符串是否符合正則規(guī)則
正則規(guī)則 rule = "HelloWorld" 字符串: s1 = "HelloKitty"; 字符串: s2 = "HelloWorld"; // s1 s2 中那個字符串符合 rule 約定的規(guī)則? boolean b1 = s1.matches(rule); //false boolean b2 = s2.matches(rule); //true
package string; public class RegDemo05 { public static void main(String[] args) { /* * 測試正則表達式 */ //定義正則表達式 String rule = "HelloWorld"; //定義被檢測的字符串 String s1 = "HelloKitty"; String s2 = "HelloWorld"; //檢測 s1 是否符合規(guī)則 boolean b1 = s1.matches(rule); //檢測 s2 是否符合規(guī)則 boolean b2 = s2.matches(rule); System.out.println(b1); System.out.println(b2); } }
字符集
匹配一個有效字符范圍。
語法:
[123456]
意義:
- 匹配一個字符
- 其有效范圍: 1 2 3 4 5 6 中的某一個
正則規(guī)則例子:
Hello[123456]
- - 匹配6個字符
- - 前5個必須是Hello
- - 第6個字符,必須 1 2 3 4 5 6 中的一個
如, 可以匹配的字符串:
- - "Hello1"
- - "Hello2"
- - "Hello3"
- - ...
- - "Hello6"
- - "Hello7" 不可以匹配!
- - “HelloA” 不可以
正則例子: 我[草去艸]
字符范圍
| 規(guī)則 | 正則表達式 | 范圍 | | ------------------ | ---------------------------- | -------- | | 匹配 0~9 一個字符 | [0123456789] | [0-9] | | 匹配A-Z一個字符 | [ABCDEFGHIJKLMNOPQRSTUVWXYZ] | [A-Z] | | 匹配a-z一個字符 | ... | [a-z] | | 匹配a-zA-Z一個字符 | | [a-zA-Z] |
栗子:
Hello[1-6]
預定義字符集
| 規(guī)則 | 正則 | 預定義字符集 | 栗子 | | ------------------ | ------------ | ------------ | ------------ | | 匹配一個數字 | [0-9] | \d | Hello\d | | 匹配一個單詞字符 | [a-zA-Z0-9_] | \w | A\w | | 匹配一個空白字符 | | \s | Hello\sWorld | | 匹配任意一個字符 | | . | A. | | 匹配一個非數字 | | \D | | | 匹配一個非空白 | | \S | | | 匹配一個非單詞字符 | | \W | |
栗子, 網站上規(guī)則 用戶名規(guī)則是6個單詞字符:
- 正則規(guī)則: \w\w\w\w\w\w
- java String: `"\\w\\w\\w\\w\\w\\w"`
測試案例:
package string; public class RegDemo07 { public static void main(String[] args) { /* * 測試 用戶名規(guī)則:6個單詞字符組成 * - \ 在java字符串中需要進行轉義為 \\ */ //正則表達式: String reg = "\\w\\w\\w\\w\\w\\w"; System.out.println(reg); //被檢查的字符串 String s1 = "Jerry1"; //可以通過檢查 String s2 = "Tom-12"; //不可以通過檢查 String s3 = "Andy"; //不可以通過檢查 System.out.println(s1.matches(reg)); System.out.println(s2.matches(reg)); System.out.println(s3.matches(reg)); } }
數量詞
約定左側元素出現的次數。
栗子:
\w\w\w\w\w\w 等價 \w{6}
語法:
- X{n} 規(guī)定左側X出現n次
- X{n,m} 規(guī)定左側X出現最少n次, 最多m次
- X{0,n} 規(guī)定左側X出現0到n次
- X{n,} 規(guī)定左側X出現最少n次
- X? 和 X{0,1} 等價,X可以沒有或者有一個
- X+ 和 X{1,} 等價,X至少有一個,多了隨意,簡稱:一個以上
- X* 和 X{0,} 等價,X至少有0個,多了隨意 簡稱:0個以上
栗子:
- - 網站的用戶名是 8~16個單詞字符: \w{8,16}
- - 網站的密碼是單詞字符, 最少8個, 多了不限: \w{8,}
- - 匹配Hello World,中間至少有一個空白: Hello\s+World
- - 不能匹配 : "HelloWorld"
- - 不能匹配: "Hello World!"
- - 能匹配: "Hello World"
- - 能匹配: "Hello World"
- - 能匹配: "Hello World"
特殊字符轉義
如何匹配字符 [ ] ? + * . , 使用 \特殊字符, 進行轉義!
\. 匹配點 \[ 匹配 [ \? 匹配 ? \* 匹配 * \+ 匹配 + \\ 匹配 \
如下正則的意義:匹配 `www.fish.cn` 域名
- www.fish.cn 匹配:
- www.fish.cn 通過
- wwwfishAcn 通過
- www-fish-cn 通過- `www\.fish\.cn` 匹配
- www.fish.cn 通過
- wwwfishAcn 不通過
- www-fish-cn 不通過
案例:如何檢查一個字符串是否為正確的IPv4地址
正確IP:
"192.168.1.25" "192.168.199.1" "10.0.0.20" "8.8.8.8"
錯誤的IP:
"10-10-10-20" "192點168點5點25"
正則:`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`
分組
講一組規(guī)則作為整體進行處理
栗子正則:
1. `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` 2. `(\d{1,3}\.)(\d{1,3}\.)(\d{1,3}\.)\d{1,3}` 3. `(\d{1,3}\.){3}\d{1,3}`
```java
package string; public class RegDemo11 { public static void main(String[] args) { /* * 檢查IP地址是否符合規(guī)則 */ //定義正則規(guī)則 //String reg = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; String reg = "\\d{1,3}(\\.\\d{1,3})(\\.\\d{1,3})(\\.\\d{1,3})"; //String reg = "(\\d{1,3}\\.){3}\\d{1,3}"; //測試分組 //定義被檢查的字符串 String ip1 = "192.168.2.70"; String ip2 = "10.0.0.20"; String ip3 = "8.8.8.8"; //定義錯誤的被檢查字符串 String ip4 = "192點168點2點70"; String ip5 = "192-168-2-70"; //檢查 System.out.println(ip1.matches(reg)); System.out.println(ip2.matches(reg)); System.out.println(ip3.matches(reg)); System.out.println(ip4.matches(reg)); System.out.println(ip5.matches(reg)); } }
栗子2:
1. `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` 2. `\d{1,3}(\.\d{1,3})(\.\d{1,3})(\.\d{1,3})` 3. `\d{1,3}(\.\d{1,3}){3}`
區(qū)別:
(\d{1,3}\.){3}\d{1,3} (分組){3} 分組的整體出現3次 \d{1,3}\.{3}\d{1,3} \.{3} .必須出現2次,可以匹配 “192...168”
正則API
- - matches 檢查字符串是否整體符合正則表達式規(guī)則
- - split 劈開
- - replaceAll 全部替換
Split 劈開字符串(重要)
將一個字符串劈開為幾個子字符串:
- - "192.168.5.140" 劈開為 "192" "168" "5" "140"
- - "1, Tom, 110, tom@tedu.cn" 劈開為 "1" "Tom" "110" "tom@tedu.cn"
使用:
str 存儲的是被劈開的字符串
正則 用于匹配劈開的位置點, 如: , 或者 \.
返回值 是劈開以后的數組,每個元素是 劈開的子字符串段落
劈開以后,匹配的位置就沒有了
String[] arr = str.split(正則);
案例:
String str = "1, Tom, 110, tom@tedu.cn"; // , , , // arr= "1" " Tom" " 110" " tom@tedu.cn" String[] arr = str.split(","); for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); }
replaceAll
- replace: 替換
- all:全部
將正則表達式匹配到的字符,都替換為新字符串
例子:
我天疫情又嚴重了,我去,又要做核算了。
需要替換為 `***疫情又嚴重了,***,又要做核算了。`
代碼:
Scanner scanner = new Scanner(System.in); System.out.print("請輸入:"); String str = scanner.nextLine(); //String str = "我天疫情又嚴重了,我去,又要做核算了。"; // str.replaceAll("正則", 替換字符串); String s = str.replaceAll("我[去草靠艸]", "***"); System.out.println(s);
總結
到此這篇關于Java中Stringbuilder和正則表達式的文章就介紹到這了,更多相關Java Stringbuilder和正則表達式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Java8?Stream流的skip?+?limit實現批處理的方法
Stream 作為 Java 8 的一大亮點,它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念這篇文章主要介紹了使用Java8?Stream流的skip?+?limit實現批處理,需要的朋友可以參考下2022-07-07SpringBoot在RequestBody中使用枚舉參數案例詳解
這篇文章主要介紹了SpringBoot在RequestBody中使用枚舉參數案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09Java實現把excel xls中數據轉為可直接插入數據庫的sql文件
這篇文章主要介紹了Java實現把excel xls中數據轉為可直接插入數據庫的sql文件 的相關資料,需要的朋友可以參考下2016-03-03Spring Security OAuth2實現使用JWT的示例代碼
這篇文章主要介紹了Spring Security OAuth2實現使用JWT的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Mybatis-Plus中IdType.AUTO局部配置不生效的問題解決
本文主要介紹了Mybatis-Plus中IdType.AUTO局部配置不生效的問題解決,數據庫插入數據時,id的默認生成方式還是雪花算法,局部配置沒有生效,下面就來解決一下,感興趣的可以了解一下2023-09-09java利用mybatis攔截器統(tǒng)計sql執(zhí)行時間示例
這篇文章主要介紹了java利用mybatis攔截器統(tǒng)計sql執(zhí)行時間示例,該攔截器攔截mybatis的query和update操作,能統(tǒng)計sql執(zhí)行時間2014-03-03基于FeignException$InternalServerError的解決方案
這篇文章主要介紹了FeignException$InternalServerError的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06