Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
Replace multiple line spacings with one line spacing?

How do I replace multiple line spacings with one line spacing in a string?

I want this:

Hello!







Hello again!

to be converted to this:

Hello!
Hello again!

or this:

Hello!

Hello again!
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use :

str.replaceAll("\n+", "\n");

Here is an example :

String str = "Hello!\n"
        + "\n"
        + "\n"
        + "\n"
        + "\n"
        + "\n"
        + "\n"
        + "\n"
        + "Hello again!";
str = str.replaceAll("\n+", "\n");
System.out.println(str);

Output

Hello!
Hello again!
over 4 years ago · Santiago Trujillo Denunciar

0

Try Bart Kiers's answer to remove empty lines within a string.

String removedEmptyLines = text.replaceAll("(?m)^[ \t]*\r?\n", "");
over 4 years ago · Santiago Trujillo Denunciar

0

You can make a Queue . Read line by line If first character of line is not a new line character , include it in the queue. Otherwise if its a space just peek last element is queue, dont add it if the queue last element was also new line .

So when you print also , you have the liberty you want to print new line character or not .

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda