基礎(chǔ)篇:String字符串操作-必知必備
本文轉(zhuǎn)載自微信公眾號(hào)「潛行前行」,作者cscw。轉(zhuǎn)載本文請(qǐng)聯(lián)系潛行前行公眾號(hào)。
前言
字符串的常用操作,記一下。如果有字符串的操作小技巧,好用的工具類,歡迎補(bǔ)全。大家隨意點(diǎn)贊,隨意看看
- String實(shí)現(xiàn)原理
- String常用方法
- 字符串的緩存池和String.intern
- 正則匹配和字符串
- StringBuffer和StringBuilder
- spring工具類StringUtils
String實(shí)現(xiàn)原理
- public final class String implements Serializable,Comparable<String>, CharSequence {
- private final char value[];
- private int hash; // Default to 0
String有一個(gè)value的字符數(shù)組,用來保存字符串的。默認(rèn)編碼是utf-8
String常用方法
- //從toffset開始,是否以prefix開頭
- public boolean startsWith(String prefix, int toffset)
- //str出現(xiàn)的最后一次的下標(biāo)
- public int lastIndexOf(String str)
- //返回下標(biāo)在beginIndex和endIndex之間的字符串
- public String substring(int beginIndex, int endIndex)
- //連接str,并返回一個(gè)新的字符串
- public String concat(String str)
- //匹配regex正則表達(dá)式
- public boolean matches(String regex)
- //替換字符
- public String replace(char oldChar, char newChar)
- //替換字符串
- public String replace(CharSequence target, CharSequence replacement)
- //獲取charsetName編碼的字節(jié)數(shù)組
- public byte[] getBytes(String charsetName)
- //獲取
- public int codePointAt(int index)
- //分割
- public String[] split(String regex, int limit)
- //轉(zhuǎn)成小寫
- public String toLowerCase()
- //轉(zhuǎn)成大寫
- public String toUpperCase()
- //截取前后空格
- public String trim()
- //elements使用delimiter作為連接符,合并成一個(gè)新字符串
- public static String join(CharSequence delimiter, CharSequence... elements)
- public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
- //args參數(shù)填充進(jìn)format的占位符,并返回新字符串
- public static String format(String format, Object... args)
- //轉(zhuǎn)為緩存池的字符串
- public native String intern()
使用示例
- String data = "String字符串操作-必知必備";
- System.out.println(data.startsWith("St",0));
- System.out.println(data.startsWith("St",5));
- ----------
- true
- false
字符串的格式化輸出
public static String format(String format, Object... args)
- System.out.println(String.format("%s-%s","wst","csc"));
- --------
- wst-csc
字符串的緩存池和String.intern
string.intern();intern先判斷常量池是否存相同字符串,存在則返回該引用;否則在常量池中記錄堆中首次出現(xiàn)該字符串的引用,并返回該引用。
如果是先執(zhí)行 String s = "st" ;相當(dāng)于執(zhí)行了intern();先在常量池創(chuàng)建"st",并且將引用A存入常量池,返回給s。此時(shí)String("hello").intern()會(huì)返回常量池的引用A返回
使用示例
- public static void main(String[] args) throws Exception {
- String one = "st";
- String two = new String("st");
- String three = two.intern();
- System.out.println(two == one);
- System.out.println(three == one);
- }
- ---------------
- false
- true
字符串和正則匹配、Matcher
字符 | 說明 |
---|---|
\ | 將下一字符標(biāo)記為特殊字符、文本、反向引用或八進(jìn)制轉(zhuǎn)義符。例如,"n"匹配字符"n"。"\n"匹配換行符,"\("匹配"(" |
^ | 匹配輸入字符串開始的位置。如果設(shè)置了 RegExp 對(duì)象的 Multiline 屬性,^ 還會(huì)與"\n"或"\r"之后的位置匹配 |
$ | 匹配輸入字符串結(jié)尾的位置。如果設(shè)置了 RegExp 對(duì)象的 Multiline 屬性,$ 還會(huì)與"\n"或"\r"之前的位置匹配 |
* | 零次或多次匹配前面的字符或子表達(dá)式。例如,zo* 匹配"z"和"zoo"。* 等效于 {0,} |
+ | 一次或多次匹配前面的字符或子表達(dá)式。例如,"zo+"與"zo"和"zoo"匹配,但與"z"不匹配。+ 等效于 {1,} |
? | 零次或一次匹配前面的字符或子表達(dá)式。例如,"do(es)?"匹配"do"或"does"中的"do"。? 等效于 {0,1} |
{n} | n 是非負(fù)整數(shù)。正好匹配 n 次。例如,"o{2}"與"Bob"中的"o"不匹配,但與"food"中的兩個(gè)"o"匹配 |
{n,} | n 是非負(fù)整數(shù)。至少匹配 n 次。例如,"o{2,}"不匹配"Bob"中的"o",而匹配"foooood"中的所有 o。"o{1,}"等效于"o+"。"o{0,}"等效于"o*" |
{n,m} | m 和 n 是非負(fù)整數(shù),其中 n <= m。匹配至少 n 次,至多 m 次。例如,"o{1,3}"匹配"fooooood"中的頭三個(gè) o。'o{0,1}' 等效于 'o?'。注意:您不能將空格插入逗號(hào)和數(shù)字之間。 |
? | 當(dāng)此字符緊隨任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后時(shí),匹配模式是"非貪心的"。"非貪心的"模式匹配搜索到的、盡可能短的字符串,而默認(rèn)的"貪心的"模式匹配搜索到的、盡可能長的字符串。例如,在字符串"oooo"中,"o+?"只匹配單個(gè)"o",而"o+"匹配所有"o"。 |
. | 匹配除"\r\n"之外的任何單個(gè)字符。若要匹配包括"\r\n"在內(nèi)的任意字符,請(qǐng)使用諸如"[\s\S]"之類的模式。 |
\n | 換行符匹配。等效于 \x0a 和 \cJ |
\r | 匹配一個(gè)回車符。等效于 \x0d 和 \cM |
\s | 匹配任何空白字符,包括空格、制表符、換頁符等 |
\d | 數(shù)字字符匹配。等效于 [0-9] |
[a-z] | 字符范圍。匹配指定范圍內(nèi)的任何字符。例如,"[a-z]"匹配"a"到"z"范圍內(nèi)的任何小寫字母 |
使用示例
- public static void main(String[] args) throws Exception {
- String content = "I am st ! ";
- String pattern = ".*st.*";
- boolean isMatch = Pattern.matches(pattern, content);
- System.out.println("字符串中是否包含了 'st' 子字符串? " + isMatch);
- }
- ---------------
- 字符串中是否包含了 'st' 子字符串? true
StringBuffer和StringBuilder
StringBuffer、StringBuilder和String一樣,也用來代表字符串。String類是不可變類,每次都要重新申請(qǐng)內(nèi)存,不過StringBuffer和StringBuilder是事先申請(qǐng)緩存內(nèi)存,在拼接變長且較長的字符串時(shí),性能比String好
StringBuffer是線程安全的,每一次加減操作都是加上鎖的;StringBuilder則是StringBuffer無鎖版本。如果對(duì)線程安全不了解,建議使用StringBuffer,不需考慮加鎖會(huì)帶來性能的損耗,因?yàn)閖vm會(huì)自動(dòng)視情況優(yōu)化為無鎖操作
spring工具類StringUtils
- public static boolean isEmpty(Object str)
- //是否含有空格符
- public static boolean containsWhitespace(CharSequence str)
- //截取兩邊空格符
- public static String trimWhitespace(String str)
- //替換所有空格符
- public static String trimAllWhitespace(String str)
- //獲取文件名
- public static String getFilename(String path)
歡迎指正文中錯(cuò)誤
參考文章
- 關(guān)于String.intern()[1]
- String的Intern方法[2]
- Java 正則表達(dá)式[3]