Home » JavaScript

JavaScript program to split the keywords in the string

Learn: How to split the keywords in the string using JavaScript code? Here is a program of JavaScript which is used to split string in keywords separated by some delimiters.
Submitted by Abhishek Pathak, on June 14, 2017

In this simple program, we will use the JavaScript to input a list of keywords from the user. The keywords will be prompted by the browser and the user will enter the keywords, separated by "," character.

From the input, we will separate the keywords using the split() method of String object. The split method takes single argument, which is the character where to split.

JavaScript code to split the keywords in the string

var sentence = prompt('Enter keywords separated by ","',"");
var keywords = sentence.split(",");

var opString = "";

keywords.forEach(function(keyword){
  keyword = keyword.trim();  
  opString += "I love " + keyword + "<br/>";
});

document.getElementById("output").innerHTML = opString;

In this program we will input the string from the user using the prompt() method. The returned value will be stored in the sentence string.

Then we will apply the split() method on the sentence string object. We will pass the character ",". Now, the method returns value in array. This value will be stored in the array variable named as keywords.

Now using forEach method on keywords array, we will print the result in the browser. For that we have to stuff the opString by concatenating with each keyword. But wait, there a simple thing that we need to take care of, which is the extra space in between the keywords. We can omit the extra space by using the trim() method on the keyword.

Finally, we print the opString by adding it to innerHTML of the output element.

Here is the output of JavaScript:

Input:

javascript code to split string input

Output:

javascript code to split string output

JavaScript code with HTML

<!DOCTYPE html>
<html>
<head>
	<title>Javascript Split String</title>
    
    <script>
		window.onload = function() {
			var sentence = prompt('Enter your hobbies separated by ","',"");
			var keywords = sentence.split(",");

			var opString = "";

			keywords.forEach(function(keyword){
			  keyword = keyword.trim();  
			  opString += "I love " + keyword + "<br/>";
			});
			document.getElementById("output").innerHTML = opString;
		}
     </script>    
</head>

<body>

    <p id="output"></p>

</body>
</html>

DEMO





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.