JavaScript - Add Characters at the start and end of each line
JavaScript - Add Characters at the start and end of each line. This program adds some characters at the start and end of each line of the input text.
CODE
<!--JQuery Reference Library-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
function addCharactersStartEnd(){
inputText = "Apple\nBanana\nOrange"; // Provide the input text here.
startCharacters = "'"; // Provide the characters to add at the start of each line.
endCharacters = "',"; // Provide the characters to add at the end of each line.
var lines = inputText.split("\n"); //Split the input based on the new line character.
var resultArray = [];
//Loop through the splitted lines.
$.each(lines, function(){
resultArray.push(startCharacters + String(this) + endCharacters); //For each line, add characters at the start and end and push it to the output array.
});
var result = resultArray.join("\r\n"); //Join all the array elements with a new line character.
alert(result);
}
addCharactersStartEnd();
//Output
//‘Apple’,
//‘Banana’,
//‘Orange’,
</script>
Comments
Post a Comment