導航:首頁 > 編程語言 > javasplit多個

javasplit多個

發布時間:2024-06-30 18:52:04

『壹』 java的split()是怎麼拆分字元串的

java拆分字元串使用string類的spilt方法,針對某個分隔符來分割一個字元串,示例如下:
public class StringSplit {
public static void main(String[] args) {
String sourceStr = "1,2,3,4,5";//一個字元串
String[] sourceStrArray = sourceStr.split(",");//分割出來的字元數組
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}

// 最多分割出3個字元串
int maxSplit = 3;
sourceStrArray = sourceStr.split(",", maxSplit);
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}
}
}
輸出結果為:

2
4
1
3,4,5

『貳』 java裡面的split方法

java和c#的split都差不多
以下是java的split的特性及一些例子:
java.lang.string.split

split 方法
將一個字元串分割為子字元串,然後將結果作為字元串數組返回。

stringObj.split([separator,[limit]])

stringObj
必選項。要被分解的 String 對象或文字。該對象不會被 split 方法修改。

separator
可選項。字元串或 正則表達式 對象,它標識了分隔字元串時使用的是一個還是多個字元。如果忽
略該選項,返回包含整個字元串的單一元素數組。

limit
可選項。該值用來限制返回數組中的元素個數。

說明:
split 方法的結果是一個字元串數組,在 stingObj 中每個出現 separator 的位置都要進行分解
。separator 不作為任何數組元素的部分返回。

示例1:
public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";
// 在每個空格字元處進行分解。
ss = s.split(" ");
}

public static void main(String[] args) {

SplitDemo demo = new SplitDemo();
for (int i = 0; i < ss.length; i++)
System.out.println(ss[i]);
}

}

程序結果:
The
rain
in
Spain
falls
mainly
in
the
plain.

示例2:
public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";
// 在每個空格字元處進行分解。
ss = s.split(" ", 2);
}

public static void main(String[] args) {
SplitDemo demo = new SplitDemo();
for (int i = 0; i < ss.length; i++)
System.out.println(ss[i]);
}

}

程序結果:
The
rain in Spain falls mainly in the plain.

示例3:
public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";
// 在每個空格字元處進行分解。
ss = s.split(" ", 20);
}

public static void main(String[] args) {
SplitDemo demo = new SplitDemo();
for (int i = 0; i < ss.length; i++)
System.out.println(ss[i]);
}

}

程序結果:
The
rain
in
Spain
falls
mainly
in
the
plain.

示例4:
public class SplitDemo {

public static void main(String[] args) {

String value = "192.168.128.33";
String[] names = value.split(".");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}

}
}

運行結果:

對,沒看錯!沒有任何輸出!
讓我們來看看 split 方法的方法簽名吧:

public string[] split(string regex)
這里的參數的名稱是 regex ,也就是 regular expression (正則表達式)。這個參數並不是一個簡單的分割用的字元,而是一個正則表達式,看了 split 方法的實現代碼就更堅定了我們的信心:

public string[] split(string regex, int limit) {
return pattern.compile(regex).split(this, limit);
}
split 的實現直接調用的 matcher 類的 split 的方法。讀者已經知道,「 . 」在正則表達式中有特殊的含義,因此我們使用的時候必須進行轉義。
只要將
String[] names = value.split(".");
改為
String[] names = value.split("//.");
就可以了。

輸出結果:
192
168
128
33

再加一點兒補充(這是Java幫助文檔中的,更清晰一些):

public String[] split(String regex,int limit)根據匹配給定的正則表達式來拆分此字元串。
此方法返回的數組包含此字元串的每個子字元串,這些子字元串由另一個匹配給定的表達式的子字元串終止或由字元串結束來終止。數組中的子字元串按它們在此字元串中的順序排列。如果表達式不匹配輸入的任何部分,則結果數組只具有一個元素,即此字元串。

limit 參數控制模式應用的次數,因此影響結果數組的長度。如果該限制 n 大於 0,則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,而且數組的最後項將包含超出最後匹配的定界符的所有輸入。如果 n 為非正,則模式將被應用盡可能多的次數,而且數組可以是任意長度。如果 n 為零,則模式將被應用盡可能多的次數,數組可有任何長度,並且結尾空字元串將被丟棄。

例如,字元串 "boo:and:foo" 使用這些參數可生成下列結果:

Regex Limit 結果

: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }

這種形式的方法調用 str.split(regex, n) 產生與以下表達式完全相同的結果:

Pattern.compile(regex).split(str, n)

參數:
regex - 定界正則表達式
limit - 結果閾值,如上所述
返回:
字元串數組,根據給定正則表達式的匹配來拆分此字元串,從而生成此數組
拋出:
PatternSyntaxException - 如果正則表達式的語法無效
從以下版本開始:
1.4

public String[] split(String regex)根據給定的正則表達式的匹配來拆分此字元串。
該方法的作用就像是使用給定的表達式和限制參數 0 來調用兩參數 split 方法。因此,結果數組中不包括結尾空字元串。

例如,字元串 "boo:and:foo" 產生帶有下面這些表達式的結果:

Regex 結果
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

參數:
regex - 定界正則表達式
返回:
字元串數組,根據給定正則表達式的匹配來拆分此字元串,從而生成此數組。
拋出:
PatternSyntaxException - 如果正則表達式的語法無效

『叄』 java Split如何去除一個空格和多個空格

JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾、中間 復制代碼 代碼如下:String str = " hell o "; String str2 = str.replaceAll(" ", ""); System.out.println(str2)

『肆』 java瀛楃︿覆split澶氫釜瀛楃︾敤+鍙蜂箞

寰楃湅浣犲瓧絎︿覆閲岄潰鐢ㄧ殑浠涔堜簡銆
String str="aaa+bbb+ccc";
榪欐牱灝辯敤"+",鏄鍟ュ氨鐢ㄥ暐銆

『伍』 java,split 如何設置多個分隔符

java中String類的split方法接受正則表達式作為參數,我們可以使用正則表達式實現多個分隔符進行分隔的效果。
示例代碼如下:

importjava.util.*;
importjava.lang.*;
importjava.io.*;

/*Nameoftheclasshastobe"Main"onlyiftheclassispublic.*/
classIdeone
{
publicstaticvoidmain(String[]args)throwsjava.lang.Exception
{
Stringstr="abc;123,456?999|haha";
String[]strs=str.split("[;,?|]");
for(Strings:strs){
System.out.println(s);
}
}
}

執行結果:
abc
123
456
999
haha

閱讀全文

與javasplit多個相關的資料

熱點內容
安卓手機怎麼下載掌上市監 瀏覽:874
hanwckf編譯教程 瀏覽:88
如何編譯圖片 瀏覽:386
obj編譯錯誤 瀏覽:226
vs編譯fortran程序 瀏覽:696
安卓微信風控怎麼解除 瀏覽:136
boa編譯配置環境出現問題 瀏覽:340
辦理解壓需要先處理違章嗎 瀏覽:557
雲伺服器怎麼掛硬碟 瀏覽:917
android閱讀sdk 瀏覽:803
如何重置安卓手機密碼 瀏覽:153
如何選壓縮面膜 瀏覽:755
世界形勢與要聞用什麼app好 瀏覽:836
程序員那麼可愛雷哥查出病是哪集 瀏覽:305
門禁控制器編程 瀏覽:234
android滑動點擊沖突 瀏覽:357
小米是加密手機嗎 瀏覽:28
房測之友加密狗 瀏覽:475
天翼雲伺服器配置埠 瀏覽:96
51單片機串列通訊 瀏覽:736