0

I am trying to achieve this using the String.split function in Java but doesn't seem to find an elegant solution. The number of blank space between each character or word is fixed.

String original = "H E L L O W O R L D" String finalWord = original.split(); System.out.println(finalWord); HELLO WORLD 

Basically, the number of white space between each alphabet is fixed e.g. 2 and the number of white space between word is also fixed e.g. 4.

How can I achieve that using 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

into

HELLO(space)WORLD

Hope it's understandable!

10
  • 1
    First, replace 4 spaces by single space. Then replace 2 spaces with blank space. Commented Nov 8, 2017 at 13:36
  • 2
    Posting sample code that doesn't compile (i.e. "final" cannot be a variable name, split needs an argument, etc.) indicates that you didn't try too hard to find the solution on your own. Commented Nov 8, 2017 at 13:39
  • 1
    Is the number of spaces fixed for all inputs, or is it one of the variable parameters of the system? Commented Nov 8, 2017 at 13:43
  • 2
    You have 5 spaces between HELLO and WOLD Commented Nov 8, 2017 at 13:46
  • Is "using the String.split" part of the requirements? Commented Nov 8, 2017 at 13:47

6 Answers 6

3
original.replaceAll("\\s{4,}", " ").replaceAll("\\s{2,}", "").trim() 

The first will replace 4+ spaces with a space and the second will replace 2+ spaces with no spaces.

Sign up to request clarification or add additional context in comments.

Comments

1

So you don't need to split it and you can simply replace 2 spaces for 1 empty and you get HELLO(space)WORLD:

String original = "H E L L O W O R L D"; String a = original.replaceAll(" ", ""); System.out.println(a); 

4 Comments

How it works exactly? I thought it would have output no space but surprisingly it showed the correct result.
This works only because OP included five spaces in their question and everyone copy-pasted the code directly from there.
Oops I am sorry its 2 space and 4 space, I have made the necessary adjustment.
Yes, the first string had 5 spaces between HELLO and WORLD so my solutions works fine, but now, with 4 spaces between words I think that better solution is @ajc
0

You can try tis.

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); } 

1 Comment

@cmy113 It was easy, don't forget to click on the top arrow, thanks :)
0

Replace 4 Spaces by 1 space and then 2 space by nothing : http://rextester.com/LURGM31649

String original = "H E L L O W O R L D"; original.replaceAll(" ", " ").replaceAll(" ", ""); 

2 Comments

@baao that's not necessarily Javascript. It might be Groovy ;-)
This code now has one issue and one flaw. Issue: you don't care about the replaceAll result (i.e. not assigned to anything). Flaw: there is no point in using replaceAll instead of replace.
0

If it is not a homework, you can try this with Java 8

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); 

Comments

0

You can pass a regular expression to the split-separator to split on two-or-three spaces, resulting in leaving the four spaces intact:

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 

2 Comments

You've put 5 spaces instead of 4 in original. If you use 4 spaces, as per OP's requirement, you get "HELLOWORLD".
@DodgyCodeException good catch, things like these cause space shuttles to crash. Adapted to the real requirement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.