在此紀錄下來
希望大家一起分享一起交流
切割字串解析成每個字元
1 . 方法一
String s = "abcd,efgh";
System.out.println("Use String method : Char Array");
char[] a = s.toCharArray();
for (char c : a) {
System.out.println(c);
}
Use String method : Char Array
a
b
c
d
,
e
f
g
h
2 . 方法二
String s = "abcd,efgh";
System.out.println("Use String method : String length and char At");
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
System.out.println(c);
}
Use String method : String length and char At
a
b
c
d
,
e
f
g
h
藉由特定的字串切割( Split )
1 . 字串切割
String s = "abcd,efgh";
System.out.println("Use String method : Split");
String[] strs = s.split(",");
for(String str: strs) {
System.out.println(str);
}
Use String method : Split
abcd
efgh
2 .字串切割,但限制條件
String s = "abcd,efgh,ijkl,mnop,qrst";
System.out.println("Use String method : Split limit");
for (String retval : s.split(",", 3)) {
System.out.println(retval);
}
Use String method : Split limit
abcd
efgh
ijkl,mnop,qrst
歡迎轉載,請註明出處。