如果找不到Java中的字符串拆分,可以尝试以下方法:
import java.util.regex.Pattern;
public class StringSplit {
public static void main(String[] args) {
String str = "Hello,World!";
String[] splitStr = Pattern.compile(",").split(str);
System.out.println(splitStr[0]); // 输出:Hello
System.out.println(splitStr[1]); // 输出:World!
}
}
public class StringSplit {
public static void main(String[] args) {
String str = "Hello,World!";
String[] splitStr = str.split(",");
System.out.println(splitStr[0]); // 输出:Hello
System.out.println(splitStr[1]); // 输出:World!
}
}
import java.util.StringTokenizer;
public class StringSplit {
public static void main(String[] args) {
String str = "Hello,World!";
StringTokenizer tokenizer = new StringTokenizer(str, ",");
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken()); // 输出:Hello, World!
}
}
}
import java.util.Scanner;
public class StringSplit {
public static void main(String[] args) {
String str = "Hello,World!";
Scanner scanner = new Scanner(str).useDelimiter(",");
while (scanner.hasNext()) {
System.out.println(scanner.next()); // 输出:Hello, World!
}
}
}
以上方法都可以实现字符串的拆分,可以根据实际需求选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云