JavaScript - Add Characters at the start of each line
JavaScript - Add Characters at the start of each line
CODE
<!--JQuery Reference Library-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
function addCharactersStart(){
inputText = "Apple\nBanana\nOrange"; // Provide the input text here.
characters = "$$$"; // Provide the characters to add at the start 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(characters + String(this)); //For each line, add characters at the start 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);
}
addCharactersStart();
//OUTPUT
//$$$Apple
//$$$Banana
//$$$Orange
</script>
Comments
Post a Comment