Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptHow to split multiline string into an array of lines in JavaScript...

How to split multiline string into an array of lines in JavaScript ?

Multiline string in JavaScript means a string having two or more lines. To split a multiline string into an array we need to use split() in our JavaScript code. 

JavaScript split(separator, limit) Method: The split() function is used to split the data depending upon the attributes we are passing in it. The separator attributes specify that from this word/sign the string will be divided. The limit attribute is optional, it specifies how many splits will be there.

Example 1: This example shows the use of the above approach. In this, when we will click on the “Go” button the array of the multiline string will be displayed on the screen separated by “,”.

HTML




<h1 style="color:green">
    Welcome to Geeks for Geeks
</h1>
<h3>
    Split multiline string into an array of lines in JavaScript
</h3>
<button onclick="myFunction()">
    Go
</button>
 
<p id="StringToArray"></p>
 
<script>
    function myFunction() {
        var string = "Are you ready?"
            + "<br>So let's get started";
     
        var array = string.split("<br>");
     
        document.getElementById("StringToArray")
            .innerHTML = array;
    }
</script>


Output:

 Split multiline string into an array of lines

 Split multiline string into an array of lines 

Example 2: Now, let’s see how to get a particular index of an array.

HTML




<h1 style="color:green">
    Welcome to Geeks for Geeks
</h1>
<h3>
    Split multiline string into an array of lines in JavaScript
</h3>
 
<button onclick="myFunction()">Go</button>
 
<p id="StringToArray"></p>
 
<script>
    function myFunction() {
        var string = "Are you ready?"
            + "<br>So let's get started";
     
        var array = string.split("<br>");
     
        document.getElementById("StringToArray")
            .innerHTML = array[1];
    }
</script>


Output:

 Split multiline string into an array of lines

 Split multiline string into an array of lines 

As we wrote array[1], hence only the 2nd line has been printed. If we will write array[2], then it will be undefined as this array contains data in the first two indexes only that are 0 and 1 respectively.

Example 3: Now, let’s try to take a string as user input.

HTML




<h1 style="color:green">
    Welcome to Geeks for Geeks
</h1>
<h3>
    split multiline string into an
    array of lines in JavaScript
</h3>
 
<textarea id="write" placeholder="Write something"
          style="height:100px;">
</textarea>
 
<button onclick="myFunction()">Go</button>
 
<p id="StringToArray"></p>
 
 
<script>
    function myFunction() {
     
        var string = document
            .getElementById("write").value;
     
        var array = string.split(".");
     
        document.getElementById("StringToArray")
            .innerHTML = array;
    }
</script>


Output:

 Split multiline string into an array of lines

 Split multiline string into an array of lines 

RELATED ARTICLES

Most Popular

Recent Comments