How to break string, replacing space by line break

Asked

Viewed 42,321 times

1

I need to Break a String(Phrase) in the proper whitespace, so that it is inserted in place of the line breaking \n escape character, in order for it to skip line-by-line. See an example logic, below:

Casa Bonita

Stay like this:

House

Beautiful

For more details, see what I have achieved so far, but I wish to do something more...

<script>
    window.onload=function(){
      campo = "Casa Bonita"
       letra = campo.split(" "); // Quebra Linha no Espaço
       for(var i in letra)
      alert(letra[i]);
    }
</script>

Well, what is this something else.. /result being displayed on alert row after row successively according to the size of the String(Phrase). However, I would like to skip each line using the \n and not <br> within a innerHTML();. Has help!

  • Nice, but there’s a way to adjust the method sort(); in order to list words in albetic order. If possible, exemplify how you would.

2 answers

9

you can use a regular expression to replace all whitespace.

var texto = "minha linda casa branca".replace(/\s/g, "\n");
alert(texto);

P.S: \s is the metacharacter for whitespace.

to accomplish a sort of the words, you will need to store the Palabras in an array, then carry out the luck and finally make a Join.

var texto = "minha linda casa branca";
var palavras = texto.split(" ");
var collator = new Intl.Collator('pt-BR', { sensitivity: "base" });
palavras.sort(function (paravra1, palavra2) {
  return collator.compare(paravra1, palavra2)
})
texto = palavras.join("\n");
alert(texto);

I used the Intl.Collator with { sensitivity: "base" }, so that when comparing the strings, the case and the acentos are "ignored", in this case a ≠ b, a = á, a = A.

  • 1

    Excellent example, just a note, I think Intl.Collator not supported by "iOS Webkit" and Safari 9 (Mac), however you can add this repository as a fallback https://github.com/andyearnshaw/Intl.js/ =) - Something else because you don’t use it like this .split(/\s/);? +1 to answer

  • @Guillhermenascimento, had using the /\s/g to be able to replace all the spaces, after all a .replace(" ", "\n") would replace only the first incidence of " ", as regards the .split(/\s/), at the time of the reply was taking into consideration only the " ", so I found it unnecessary to use the /\s/ (a.ka. [\f\n\r\t\v\u00a0\u1680\u180e\u2000\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])

  • 1

    "minha linda casa \t branca".replace(/\s/g, "\n"); returns several blank lines. If the need for AP is to break by phrase words, it would look better using /\s+/g to consider multiple spacing as a single block.

2

Good, your contribution has helped in full, now put my feat, so that it stays for future analysis and study by other users who as well as I have the same doubts. Behold:

texto = "Bruna Dora Carla Amanda Emiliana"
letra = texto.split(" ");
letra.sort();
texto = letra.join("\n");
alert(texto);
  • 2

    The .split also supports regex, could use so .split(/\s/), that way he’ll consider Tabs too =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.