Explanation:
On October 31, around twelve o'clock at night, the lights were dull. It was Siberian cold and I was walking painfully under the whitish lighting of a lamppost and the retro neon lights of a convenience store in a neighborhood that was buzzing when I went to buy meat. buildings, grayish in this half-light, crumbled under the weight of sleep, while the neighborhood was still open. Its decoration was particularly macabre in tone: synthetic but very sticky spider webs adorned the porches of the terraces, pumpkin ornaments are exhibited like works of art, ... Some pushed the macabre by hanging mannequins made up in corpses. At the entrance to the neighborhood, I found the body of its owner stabbed. Frozen in place, I wondered what else awaits me. My heart was pounding, feeling a shiver of anguish. When I was removing the phone from my pocket to call my police friends, my hands were clenched in fear. An investigation was carried out to find out the causes of death. Also, I went with them so they take my words. Hopefully the murderer will be arrested to put an end to this sybilinous crime.
Answer:
This article shows how to use regex to remove spaces in between a String.
A string with spaces in between.
String text = "Hello World Java.";
We want to remove the spaces and display it as below:
Hello World Java.
1. Java regex remove spaces
In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.
Regex explanation.
`\\s` # Matches whitespace characters.
+ # One or more
StringRemoveSpaces.java
package com.mkyong.regex.string;
public class StringRemoveSpaces {
public static void main(String[] args) {
String text = "Hello World Java.";
String result = text.replaceAll("\\s+", " ");
System.out.println(result);
}
}
Output
Terminal
Hello World Java.