public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String read = scan.readLine();
String str = read + ":" + "world";
String[] sets = str.split(":");
System.out.println(sets[0] + sets[1]);
}
在这里,如果我们输入hello
,我会得到hello world
。但是,当用户输入具有“:”的数据时,输入的字符串也会被拆分,并且“:”将不会被打印。如何不分割包含“:”的输入数据?
请勿在用户可以输入自己的相同字符上分割。
即使可能太多,您也可以确定使用uuid作为分隔符不会再次发生。
Scanner scan = new Scanner(System.in);
String read = scan.readLine();
String separator = UUID.randomUUID().toString();
String str = read + separator + "world";
String[] sets = str.split(separator);
System.out.println(sets[0] + sets[1]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句