[Java] StringTokenizer()、split()、substring()
StringTokenizer()分割
split() 分割
substring() 擷取
Code1:
public static void main(String[] args)
{
StringTokenizer st = new StringTokenizer("www google com"); //預設以" \t\n\r\f"為分隔符號
while(st.hasMoreTokens())
{
System.out.println("Token : " + st.nextToken());
}
}
輸出:
Token : WWW
Token : google
Token : com
Code2:
public static void main(String[] args)
{
StringTokenizer st = new StringTokenizer("www.google.com","."); //指定用"."來分割
while(st.hasMoreElements())
{
System.out.println("Token : " + st.nextToken());
}
}
輸出:
Token : WWW
Token : google
Token : com
split() 分割
String line = "A, B, C, D,1,2,3";
String[] AfterString = line.split(","); //指定用","來分割
for (int i = 0; i < AfterString.length; i++)
{
System.out.println(AfterString [i]);
}
輸出:
A
B
C
D
1
2
3
substring() 擷取
String line = "ABCDDCBA ";
String newline1 = line.substring (4);
System.out.println(newline1);
String newline2 = line.substring (0,4);
System.out.println(newline2);
輸出:
DCBA
ABCD
By Hao★
留言
張貼留言
歡迎留言