Javascript - Remove last character from each line


Javascript - Remove last character from each line

CODE
<!--JQuery Reference Library--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> function removeLastCharacter(){ inputText = "Apple\nBanana\nOrange"; // Provide the input text here. 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(String(this).substring(0, String(this).length-1)); //Removes last character from each line and add it to the result array. }); var result = resultArray.join("\r\n"); //Join all the array elements with a new line character. alert(result); } removeLastCharacter(); //Output //Appl //Banan //Orang </script>

Comments