JavaScript - Trim spaces


Javascript - Trim spaces. This program trim spaces from each of the lines.

CODE
<!--JQuery Reference Library--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> function trimSpaces(){ inputText = "Apple \n Banana\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).trim()); //Trim the space from both the ends 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); } trimSpaces(); //Output //Apple //Banana //Orange </script>

Comments