[英]How can I split a string in to multiple parts?
我有一個字符串,其中有幾個單詞用空格分隔,例如“firstword second third”和一個ArrayList。 我想將字符串拆分成幾個部分,並將'piece'字符串添加到ArrayList中。
例如,“firstword second third”可以拆分為三個單獨的字符串,因此ArrayList將有3個元素; “1 2 3 4”可以分成4個字符串,在ArrayList的4個元素中。 請參閱以下代碼:
public void separateAndAdd(String notseparated) {
for(int i=0;i<canBeSepartedinto(notseparated);i++{
//what should i put here in order to split the string via spaces?
thearray.add(separatedstring);
}
}
public int canBeSeparatedinto(String string)
//what do i put here to find out the amount of spaces inside the string?
return ....
}
如果你不理解我的意思,請發表評論或者我應該在這篇文章中修正一些錯誤。 謝謝你的時間!
您可以使用split()
在空格處拆分String:
String[] parts = inputString.split(" ");
然后迭代數組並將各個部分(如果!"".equals(parts[i]
)添加到列表中。
如果要拆分一個空格,可以使用.split(" ");
。 如果要拆分連續的所有空格,請使用.split(" +");
。
請考慮以下示例:
class SplitTest {
public static void main(String...args) {
String s = "This is a test"; // note two spaces between 'a' and 'test'
String[] a = s.split(" ");
String[] b = s.split(" +");
System.out.println("a: " + a.length);
for(int i = 0; i < a.length; i++) {
System.out.println("i " + a[i]);
}
System.out.println("b: " + b.length);
for(int i = 0; i < b.length; i++) {
System.out.println("i " + b[i]);
}
}
}
如果您擔心非標准空格,可以使用"\\\\s+"
而不是" +"
,因為"\\\\s"
將捕獲任何空格,而不僅僅是“空格字符”。
所以你的單獨和添加方法變成:
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
theArray.add(s);
}
}
這是一個更完整的示例 - 請注意,我在測試期間發現的separateAndAdd
方法中有一個小的修改。
import java.util.*;
class SplitTest {
public static void main(String...args) {
SplitTest st = new SplitTest();
st.separateAndAdd("This is a test");
st.separateAndAdd("of the emergency");
st.separateAndAdd("");
st.separateAndAdd("broadcast system.");
System.out.println(st);
}
ArrayList<String> theArray = new ArrayList<String>();
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
if(!s.isEmpty()) theArray.add(s);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(String s : theArray)
sb.append(s).append(" ");
return sb.toString().trim();
}
}
做這個:
thearray = new ArrayList<String>(Arrays.asList(notseparated.split(" ")));
或者如果thearray
已經實例化了
thearray.addAll(Arrays.asList(notseparated.split(" ")));
我建議使用
apache.commons.lang.StringUtils庫。
它是最簡單的,涵蓋了你可以想要的所有不同的條件,他用最少的代碼分割一個字符串。
以下是拆分方法的參考: 拆分方法
您還可以參考同一鏈接上可用於拆分方法的其他選項。
你可以使用.split()
試試這個
String[] words= inputString.split("\\s");
如果你想在不同的部分拆分字符串,就像這里我將告訴你我如何在日,月和年分割這個字符串14-03-2016 。
String[] parts = myDate.split("-");
day=parts[0];
month=parts[1];
year=parts[2];
試試這個:字符串為2部分:
public String[] get(String s){
int l = s.length();
int t = l / 2;
String first = "";
String sec = "";
for(int i =0; i<l; i++){
if(i < t){
first += s.charAt(i);
}else{
sec += s.charAt(i);
}
}
String[] result = {first, sec};
return result;
}
例:
String s = "HelloWorld";
String[] res = get(s);
System.out.println(res[0]+" "+res[1])
輸出:
Hello World
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.