[問題] 分割字串的問題, 求救 >"<
以下是目前我測試的程式碼, 但不是我要的結果 >"<
public static void main(String[] args) {
String str = "小尚,\"Abby, Sun\",girl,0911-XXX0L9,si,1988/11/20";
String[] cells = str.split(",");
System.out.println(str);
for(int i = 0 ; i < cells.length ; i++){
System.out.println("cells["+i+"]:"+cells[i]);
}
}
Console output:
小尚,"Abby, Sun",girl,0911-XXX0L9,si,1988/11/20
cells[0]:小尚
cells[1]:"Abby
cells[2]: Sun"
cells[3]:girl
cells[4]:0911-XXX0L9
cells[5]:si
cells[6]:1988/11/20
我想要的結果是
cells[0]:小尚
cells[1]:"Abby, Sun"
cells[2]:girl
cells[3]:0911-XXX0L9
cells[4]:si
cells[5]:1988/11/20
說明:
資料都是用逗號來當分隔符號
但是如果資料裡有逗號想呈現
就會用雙引號括住
後來想用indexOf來做
但做到
String str = "小尚,\"Abby, Sun\",girl,0911-XXX0L9,si,1988/11/20";
int idxStart;
int idxEnd;
String strTemp;
if(str.indexOf("\"")>-1){
idxStart = str.indexOf("\"");
System.out.println(str.indexOf("\""));
idxEnd = str.indexOf("\"", idxStart+1);
strTemp = str.substring(idxStart+1, idxEnd);
System.out.println(strTemp);
}
就沒想法了
而進階的資料可能會像
String str =
"小尚,\"Abby, Sun\",girl,0911-XXX0L9,\"si, Tokyo, Relax~~\",1988/11/20";
這樣的形式
不知道可不可以請教一下思路呢? 謝謝~~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.248.193
推
05/02 16:01, , 1F
05/02 16:01, 1F
→
05/02 18:05, , 2F
05/02 18:05, 2F
→
05/03 09:02, , 3F
05/03 09:02, 3F
→
05/03 09:59, , 4F
05/03 09:59, 4F
→
05/03 10:00, , 5F
05/03 10:00, 5F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 6 篇):