前言
每種語言都會有字符串的操作,因為字符串是我們平常開發使用頻率最高的一種類型。今天我們來聊一下java的字符串操作及在某些具體方法中與c#的不同,對于需要熟悉多種語言的人來說,作為一種參考。進行誡勉
首先,什么是字符串?
字符串是字符的序列,是作為一種對象而存在。說的直白點,字符串就是一些字符的組合,從而構成字符串,例如“abc”就是字符串,"郭志奇"也是一種賜福穿。
我們知道,java是一種面向對象的高級程序語言。所有事物均為對象,字符串也不例外,也是一種對象,其對應類型為string。
下面,我們分幾種情況來進行說明字符串:
1、字符串的創建
2、字符串操作
- 字符串連接
- 字符串長度
- 字符串查找
- 獲取指定索引的字符
- 去重空格
- 判斷相等
- 大小寫轉換
- 字符串分隔
- 字符串比較
字符串創建
字符串必須先聲明并進行初始化,才能使用,否則編譯器會報出 變量未初始化錯誤。
請注意:java中字符串定義的類型為string(第一個字母大寫),對于c#程序員來說,看著不太舒服,因為c#中字符串定義為string (第一個字母小寫),但c#中也是有string類的
創建字符串也分幾種方式,可以根據需求靈活組織:
string(char a[])--java代碼
1
2
3
|
char a[] = { 'a' , 'b' , 'c' }; string source = new string(a); system.out.println(source); //輸出結果為abc |
string(char a[],int offset,int length)--java代碼
1
2
3
|
char a[] = { 'a' , 'b' , 'c' }; string source = new string(a, 1 , 2 ); system.out.println(source); //輸出為bc |
字符串常量:
string source=“郭志奇”;
1
2
3
4
|
string source = "郭志奇" ; string source1 = "郭志奇" ; system.out.println(source == source1); // 輸出true system.out.println(source.equals(source1)); // 輸出true |
此處簡單提一下,java中雙等號== 表示的是引用是否相同,equals比較的是實際的字符串值。
對應的c#代碼
1
2
3
4
5
6
|
string source = "郭志奇" ; string source1 = "郭志奇" ; console.writeline(source==source1); //輸出true console.writeline(source.equals(source1)); //輸出true console.writeline(string.referenceequals(source,source1)); //輸出true console.read(); |
在c#中雙等號以及equals比較的是實際的值,而refreshequals比較的是引用是否相同。和java略有不同。
字符串操作
字符串連接
通過加號+可以將兩個字符串進行連接。請注意,字符串與其他類型連接,會自動調用其tostring方法轉換成字符串,然后進行拼接。
java代碼:
1
2
3
4
5
6
|
string source = "郭志奇" ; string source2 = "郭語晨" ; string source3 = source + source2; system.out.println(source3); // 輸出郭志奇郭語晨 string source4 = source + 521 ; // 字符串與數字拼接 system.out.println(source4); //輸出郭志奇521 |
concat:字符串拼接
1
2
3
4
|
string source = "郭志奇" ; string source2 = "郭語晨" ; string source5 = source.concat(source2); system.out.println(source5); // 輸出郭志奇郭語晨 |
字符串長度
通過length()方法獲取字符串長度。請注意:包含字符串中空格的長度。
java代碼:
1
2
|
string source = " 郭志奇 " ; //前綴包含一個空格及一個tab 后綴包含一個空格 system.out.println(source.length()); // 輸出6 |
c#代碼:
找茬:從c#輸出的長度可以看到,tab在java中長度為1,而在c#長度為2. 英文tab
1
2
|
string source = " 郭志奇 " ; console.writeline(source.length); //輸出7 |
字符串查找
兩種字符串查找方法:
indexof():從字符串開始位置進行查找
1
2
3
|
string source = "郭志奇" ; system.out.println(source.indexof( "郭" )); // 輸出0 system.out.println(source.indexof( "" )); // 輸出0 |
lastindexof()方法:從字符串結束位置開始查找
1
2
3
|
string source = "郭志奇" ; system.out.println(source.lastindexof( "郭" )); // 輸出0 system.out.println(source.lastindexof( "" )); // 輸出3 |
找茬:indexof查找空字符串,返回值是0。lastindexof方法查找空格,返回值是3.等于字符串的長度。
查看lastindexof對應的c#代碼實現:
1
2
3
4
|
string source = "郭志奇" ; console.writeline(source.length); //輸出3 console.writeline(source.lastindexof( "" )); //輸出2 console.read(); |
通過對比lastindexof查找空字符串,可以發現c#和java的不同,java返回的是字符串的長度,而c#返回的是字符串長度-1。個人認為c#的處理更符合目的。
獲取指定索引的字符
通過charat方法獲取指定索引的字符
1
2
3
|
string source = "郭志奇" ; system.out.println(source.charat( 0 )); //輸出郭 system.out.println(source.charat(source.length() - 1 )); //輸出奇 |
為了學習,我們就要思考,假如我輸入了負數或大于字符串長度的索引會出現什么呢?代碼驗證
1
2
3
|
string source = "郭志奇" ; system.out.println(source.charat(- 1 )); system.out.println(source.charat(source.length() + 11 )); |
沒錯,代碼執行出錯了,拋出異常:java.lang.stringindexoutofboundsexception
去重空格
通過trim方法移除字符串前后空格(包含tab)
1
2
3
4
5
6
|
string source = " 郭志奇 " ; // 前后都有空格 string source1 = " 郭志奇 " ; system.out.println(source.length()); system.out.println(source.trim().length()); system.out.println(source1.length()); system.out.println(source1.trim().length()); |
令人遺憾的是,java沒有提供只清空前綴空格或后綴空格的方法。而c#卻有。
判斷相等
兩種判斷字符串相等的方法,雙等號及equals、equalsignorecase
通過代碼來進行說明:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
string source = new string( "郭志奇a" ); string source1 = new string( "郭語晨" ); string source2 = "郭志奇a" ; string source3 = "郭語晨" ; string source4 = "郭志奇a" ; system.out.println(source == source2); // false system.out.println(source.equals(source2)); // true system.out.println(source1 == source3); // false system.out.println(source1.equals(source3)); // true system.out.println(source == source4); // false system.out.println(source.equals(source4)); // false system.out.println(source.equalsignorecase(source4)); // true |
雙等號代表的是字符串引用是否相同,對于不同對象的字符串,雙等號比較是不同的。
equals比較的是字符串的值,但區分大小寫
equalsignorecase:比較的是字符串的值,不區分大小寫
進行字符串比較,盡量避免使用雙等號。
大小寫轉換
touppercase轉換成大寫形式,tolowercase轉換成小寫形式。但對于數字、空格則會忽略,不進行轉換。比較簡單
字符串分隔
通過split方法進行字符串分隔,split參數是分隔符,返回值是分割后的字符串數組。較簡單。
字符串比較
通過compareto方法進行字符串比較,
1
2
3
4
5
6
7
8
9
10
|
string source = "ab" ; string source1 = "ba" ; string source2 = "ab" ; system.out.println(source.compareto(source1)); //返回值-1 system.out.println(source.compareto(source2)); //返回值0 system.out.println(source1.compareto(source2)); //返回值1 system.out.println(source.comparetoignorecase(source1)); //返回值-1 system.out.println(source.comparetoignorecase(source2)); //返回值0 system.out.println(source1.comparetoignorecase(source2)); //返回值1 |
compareto方法會逐個字符進行比較,如果源字符串比目標字符串大,那么返回正整數;如果源字符串與目標字符串相同,那么返回0;如果源字符串比目標字符串小,那么返回負數。
請注意:
1、返回值為0,那么字符串通過equals比較會相同
2、返回值正整數、負數,是源字符串不同的字符相減所得。
字符串使我們日常開發使用頻率最高的類型,因此要數量掌握字符串的各種操作,以及一些坑點,才能在編碼中輕松、愉快的避免各種bug的出現。
本次說明只介紹了字符串的冰山一角,對于更重要的字符串格式化,本次沒有涉及,因為蛋蛋字符串格式化,就可以單獨寫一篇更長的博文才能說明。下次再聊。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/jiagoushi/p/10061787.html