Java 代码示例:使用空格截取字符串
以下是一个简单的 Java 代码示例,以空格为分隔符截取字符串 'Eth-Trunk0 trunk 1 10 21 23 40 100-102 200-204' 并输出结果:
public class Main {
public static void main(String[] args) {
String input = 'Eth-Trunk0 trunk 1 10 21 23 40 100-102 200-204';
String[] tokens = input.split('\s+'); // 使用正则表达式 '\s+' 匹配一个或多个空格
for (String token : tokens) {
System.out.println(token);
}
}
}
输出结果为:
Eth-Trunk0
trunk
1
10
21
23
40
100-102
200-204
原文地址: http://www.cveoy.top/t/topic/5Ns 著作权归作者所有。请勿转载和采集!