AngularJs remove spaces from string


AngularJs remove spaces from string- There are many ways to remove blank spaces from the string. You can use .replace() to replace all blank spaces occurrence from the string. Here in this tutorial we are going to explain how you can remove blank | NULL | empty spaces. You can use our online demo try and edit the code online.


AngularJs remove spaces from string | Example

You can use replace() method to remove the blank spaces from the string. The regular expression replace(/ /g,””) is used to remove all blank spaces from the given string. Here is full example –

AngularJs remove spaces from string | Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.myString1 = "Wel Com e";
 $scope.myString2 = "";
 $scope.removeSpace = function() {
	 $scope.myString2 = $scope.myString1.replace(/ /g,"");
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
 With Space = {{myString1}}<br>
 Without Space = {{myString2}}<br>
 <button ng-click='removeSpace()' >Remove Blank Space</button>

</div> 
</div>
</body>  
</html>      


Try it »

If you run the above example it will produce the output something like this –

AngularJs remove spaces from string


Advertisements

Add Comment

📖 Read More