Java基礎學習筆記(六)——String

Java基礎學習筆記(六)—— String

Life was like a box of chocolates, you never know what you’re gonna get.

| @Author:TTODS


String是java中不可變字符串類。屬於java.lang 包,它是java非常重要的類。
所謂不可變字符串類,與之對應的是可變字符串,他們的區別在於當字符串進行拼接等修改操作時,不可變字符串會創建新的字符串對象,爲可變字符串不會創建新對象。

此文章只記錄了String 的一些常用操作,更多詳情,請查閱官方文檔:http://www.matools.com/api/java8

String的構造方法
String()

創建一個空字符序列,但由於java中的String是不可變的,所以此構造方法是不必要的。

String str = new String();
String(String original)

使用另一個String對象,進行對本進行初始化

String str = "This is str";
String str1 = new String(str);
String(char[] value)

使用字符串數組初始化String對象

char[] chs = {'H','e','l','l','o',',', 'W','o','r','l','d'};
String str = new String(chs);
System.out.print(str);//輸出Hello, World
String(char[] value,int offset,int count)

使用字符數組的索引爲offset開始,長度爲count的子數組初始化String對象。
當offset和offset+count-1不在char數組的索引範圍內時會報錯
IndexOutOfBoundsException

char[] chs = {'H','e','l','l','o',',', 'W','o','r','l','d'};
String str = new String(chs,6,5);
System.out.print(str);//輸出 World
String(byte[] bytes)
使用平臺默認的字符集來解碼默認的字節數組
byte[] byteArray = {97,98,99,100,101,102};
String str = new String(byteArray);
System.out.print(str);//輸出 abcde
String的常用方法
length()

返回字符串中字符的數量

String str = new String("Hello, World");
System.out.print(str.length());//輸出 12(逗號後面有空格)
isEmpty()

判斷字符串是否爲空,爲空就返回True,否則返回Flase

charAt(int index)

當(index<0||index>=str.length())時,報錯IndexOutOfBoundsException

String str = new String("Hello, World");
System.out.print(str.charAt(7));//輸出W
equals(Object anObject)

比較兩個字符串對象的內容是否相等,java中對於引用數據類型,"=="比較的是對象本身,即存儲對象的首地址,下例中str1與str2是兩個不同的對象,所以str1==str2返回false.

String str1 = new String("Hello, world");
String str2 = new String("Hello, world");
System.out.println(str1.equals(str2));//輸出true
System.out.print(str1==str2);//輸出false 
contentEquals(StringBuffer sb)
equalsIgnoreCase(String anotherString)

比較兩個字符串對象的內容是否相等(忽略大小寫)

String str1 = new String("Hello, world");
String str2 = new String("HELLO, WORLD");
System.out.println(str1.equals(str2));//false
System.out.print(str1.equalsIgnoreCase(str2));//true
compareTo(String anotherString)

字符串的字典序比較,相同返回0,該String字典序小於anotherString,返回一個負數,大於則返回一個正數,對於該數字的值分幾種情況:
① 兩字符串相等,返回0(如下面第1行輸出);
② 兩字符串在索引區間[0,shortStr.length()-1)(shortStr是長度較短的字符串)內有不同字符,則返回第一個不同字符的差(如下面第2、3行輸出)。
③ 兩字符串在索引區間[0,shortStr.length()- 1)(shortStr是長度較短的字符串)內沒有有不同字符,但是兩字符串的長短不一,則返回兩字符串的長度差(如下面第四行輸出)

String str1 = new String("Hello, World");
String str2 = new String("Hello, World");
String str3 = new String("ABCDE");
String str4 = new String("abcde");
String str5 = new String("Hello, World. Hello, TTODS..");
System.out.println(str1.compareTo(str2));//輸出 0 (兩個字符串的內容相等)
System.out.println(str3.compareTo(str4));//輸出 - 32  ('A'- 'a')
System.out.println(str4.compareTo(str3));//輸出 32   ('a'-'A')
System.out.println(str1.compareTo(str5));//輸出 - 16
startsWith(String prefix)

判斷字符串是否以特定的前綴開頭

startsWith(String prefix,int toffset)

判斷字符串的子串[toffset:]是否以特定的前綴開頭

 String str = new String("Hello, world. Hello, TTODS..");
System.out.println(str.startsWith("Hel"));//true
System.out.print(str.startsWith("Hel",14));//true
endsWith(String suffix)

測試此字符串是否以指定的後綴結尾。

indexOf(int ch)

在字符串中查找字符ch, 返回找到的第一個的字符ch的索引,若沒有找到則返回-1;

indexOf(int ch, int fromIndex)

在字符串中從索引fromIndex開始查找字符ch, 返回找到的第一個的字符ch的索引,若沒有找到則返回-1;

String str = new String("Hello, world. Hello, TTODS..");
System.out.println(str.indexOf('H'));//輸出 0
System.out.println(str.indexOf('H',1));//輸出 14
lastIndexOf(int ch)

在字符串中從後往前查找字符ch, 返回字符串中索引第一個的字符ch的索引,若沒有找到則返回-1;

lastIndexOf(int ch, int fromIndex)

在字符串中從fromIndex開始從後往前查找字符ch, 返回找到的第一個的字符ch的索引,若沒有找到則返回- 1;

indexOf(String str)

在字符串中查找字符串str, 返回找到的第一個的字符ch的索引,若沒有找到則返回- 1;

indexOf(String str,int fromIndex)

在字符串中從後往前查找字符串str, 返回字符串中索引第一個的字符ch的索引,若沒有找到則返回-1;

String str = new String("Hello, world. Hello, TTODS..");
System.out.println(str.indexOf("Hello"));// 輸出 0
System.out.println(str.indexOf("Hello",1));// 輸出 14
lastIndexOf(String str)

在字符串中從後往前查找字符串str, 返回字符串中索引第一個的字符ch的索引,若沒有找到則返回-1;

lastIndexOf(String str,int fromIndex)

在字符串中從fromIndex開始從後往前查找字符串str, 返回找到的第一個的字符ch的索引,若沒有找到則返回-1;

substring(int beginIndex)

類似於python中的str[beginIndex:],獲取String 索引beginIndex(包含)到最後的子串

substring(int beginIndex,int endIndex)

類似於python中的str[beginIndex:endindex],獲取String 索引beginIndex(包含)到endindex(不包含)的子串

String str = new String("Hello, world. Hello, TTODS..");
System.out.println(str.substring(14));
System.out.println(str.substring(0,14));
concat(String str)

將參數字符串連接到此字符串結尾,注意由於是不可變的,當參數字符串長度不爲0時,將創建一個新的String對象

String str1 = new String("Hello, World! ");
String str11 = new String("Hello, World");
System.out.println(str1 == str1.concat("")); //輸出 true (參數字符串長度爲0,返回字符串本身)
String str2 = new String("Hello, TTODS.!");
System.out.println(str1.concat(str2)); //輸出 Hello, World! Hello, TTODS.!
System.out.print(str11==str11.concat(str2)); //輸出 false (參數字符串長度不爲0,拼接後返回的對象是一個新建的對象,並不是原來的String對象)
replace(char oldChar,char newChar)

用newChar替代字符串中的oldChar。

String str = new String("He??o, Wor?d!");
System.out.print(str.replace('?', 'l')); //輸出 Hello, world!
matches(String regex)

判斷字符串是否匹配一個正則表達式,值得注意的時,正則表達式中的'\'在String中需要轉義即"\\"

String str = new String("2020年02月26日");
System.out.println(str.matches("\\d{4}年\\d{2}月\\d{2}日"));
contains(CharSequence s)

當且僅當此字符串包含指定的char值序列時才返回true。

replaceFirst(String regex,String replacement)

用指定字符串代替正則表達式匹配到的第一個子字符串

replaceAll(String regex,String replacement)

用指定字符串代替正則表達式匹配到的所有子字符串

String str = new String("2020年02月26日");
System.out.println(str.replaceAll("[年月日]"," "));//輸出 2020 02 26
split(String regex)

將此字符串拆分爲給定的regular expression的匹配。

String str1 = new String("2020年02月26日");
String[] strArray = str1.split("[年月日]");
for(String str :strArray)
	System.out.printf("%s\n",str);

輸出

2020
02
26

join(CharSequence delimiter,CharSequence… elements)

用指定的定界符,連接多個字符序列

String str = String.join("","Do","one","thing","at","a","time","and","do","well!");
System.out.print(str);
Do one thing at a time and do well!
toLowerCase()

將字符串中的大寫轉化爲小寫

toUpperCase()

將字符串中的小寫轉化爲大寫

trim()

去掉字符串首尾空格

format(String format,Object… args)

使用指定的格式字符串和參數返回格式化的字符串。

String str = String.format("%4d年%2d月%2d日",2020,2,26);
System.out.print(str);// 輸出 2020年 2月26日

- THE END -
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章