如何根据白色的数量拆分字符串 space
How to split a string based on number of white space
我正在尝试使用 Java 中的 String.split 函数来实现此目的,但似乎没有找到完美的解决方案。每个字符或单词之间的空格数space是固定的。
String original = "H E L L O W O R L D"
String finalWord = original.split();
System.out.println(finalWord);
HELLO WORLD
基本上,每个字母之间的白色space数量是固定的,例如2 和单词之间的白色 space 的数量也是固定的,例如4.
如何使用 Java 实现?
H(space)(space)E(space)(space)L(space)(space)L(space)(space)O(space)(space)(space)(space)W (space)(space)O(space)(space)R (space)(space)L(space)(space)D
进入
你好(space)世界
希望可以理解!
您可以将正则表达式传递给拆分分隔符以拆分两个或三个空格,从而使四个空格保持不变:
String input = "H E L L O W O R L D";
String[] parts = input.split("\W{2,3}"); // [H, E, L, L, O, , W, O, R, L, D]
System.out.println(String.join("", parts)); // HELLO WORLD
将 4 个空格替换为 1 space,然后将 2 space 替换为空:
http://rextester.com/LURGM31649
String original = "H E L L O W O R L D";
original.replaceAll(" ", " ").replaceAll(" ", "");
所以你不需要拆分它,你可以简单地将 2 spaces 替换为 1 个空的,你得到 HELLO(space)WORLD:
String original = "H E L L O W O R L D";
String a = original.replaceAll(" ", "");
System.out.println(a);
original.replaceAll("\s{4,}", " ").replaceAll("\s{2,}", "").trim()
第一个将用 space 替换 4+ spaces,第二个将用没有 spaces 替换 2+ spaces。
你可以试试。
public static void main(String[] args) throws IOException {
//BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//String line =reader.readLine();
String line = "H E L L O W O R L D";
String finalString;
finalString = line.replaceAll(" ", "");
finalString = finalString.replaceAll(" ", " ");
System.out.println(finalString);
}
如果不是作业,可以用Java8
试试
String original = "H E L L O W O R L D";
String str = Stream.of(original.split("\s{4}"))
.map(x-> x.replaceAll("\s{2}", ""))
.collect(Collectors.joining(" "));
System.out.println(str);
我正在尝试使用 Java 中的 String.split 函数来实现此目的,但似乎没有找到完美的解决方案。每个字符或单词之间的空格数space是固定的。
String original = "H E L L O W O R L D"
String finalWord = original.split();
System.out.println(finalWord);
HELLO WORLD
基本上,每个字母之间的白色space数量是固定的,例如2 和单词之间的白色 space 的数量也是固定的,例如4.
如何使用 Java 实现?
H(space)(space)E(space)(space)L(space)(space)L(space)(space)O(space)(space)(space)(space)W (space)(space)O(space)(space)R (space)(space)L(space)(space)D
进入
你好(space)世界
希望可以理解!
您可以将正则表达式传递给拆分分隔符以拆分两个或三个空格,从而使四个空格保持不变:
String input = "H E L L O W O R L D";
String[] parts = input.split("\W{2,3}"); // [H, E, L, L, O, , W, O, R, L, D]
System.out.println(String.join("", parts)); // HELLO WORLD
将 4 个空格替换为 1 space,然后将 2 space 替换为空: http://rextester.com/LURGM31649
String original = "H E L L O W O R L D";
original.replaceAll(" ", " ").replaceAll(" ", "");
所以你不需要拆分它,你可以简单地将 2 spaces 替换为 1 个空的,你得到 HELLO(space)WORLD:
String original = "H E L L O W O R L D";
String a = original.replaceAll(" ", "");
System.out.println(a);
original.replaceAll("\s{4,}", " ").replaceAll("\s{2,}", "").trim()
第一个将用 space 替换 4+ spaces,第二个将用没有 spaces 替换 2+ spaces。
你可以试试。
public static void main(String[] args) throws IOException {
//BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//String line =reader.readLine();
String line = "H E L L O W O R L D";
String finalString;
finalString = line.replaceAll(" ", "");
finalString = finalString.replaceAll(" ", " ");
System.out.println(finalString);
}
如果不是作业,可以用Java8
试试String original = "H E L L O W O R L D";
String str = Stream.of(original.split("\s{4}"))
.map(x-> x.replaceAll("\s{2}", ""))
.collect(Collectors.joining(" "));
System.out.println(str);