How to slice a string in AngularJS ?

Given a string and the task is to make a slice of given string using AngularJS.

Approach 1 Using slice() method: The approach is to use the slice() method which accepts 2 parameters start and end. In the first example, only 1 parameter is used and in the second example 2 parameters are used.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller",
            function ($scope) {
                $scope.str = "This is GeeksForGeeks";
                $scope.res = '';
                $scope.sliceStr = function () {
                    $scope.res = $scope.str.slice(8);
                }
            });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to slice a string in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            String - '{{str}}'<br><br>
            <button type="button" ng-click="sliceStr()">
                Click Me
            </button>
              
            <p>Result = '{{res}}'</p>
        </div>
    </div>
</body>
  
</html>    

chevron_right


Output:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
      
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.str = "A Computer Science portal";
            $scope.res = '';
            $scope.sliceStr = function () {
                $scope.res = $scope.str.slice(2, 18);
            }
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to slice a string in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            String - '{{str}}'<br><br>
            <button type="button" ng-click="sliceStr()">
                Click Me
            </button>
            <p>Result = '{{res}}'</p>
        </div>
    </div>
</body>
  
</html>

chevron_right


Output:

Approach 2 Using substr() method: The approach is to use the substr() method. It takes 2 parameters, one is the start and another is length(optional). In the first example only one parameter is used. And in the second example both parameters are used. 

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller",
            function ($scope) {
                $scope.str = "This is GeeksForGeeks";
                $scope.res = '';
                $scope.subStr = function () {
                    $scope.res = $scope.str.substr(8);
                }
            });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to slice a string
        using substr() method in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            String - '{{str}}'<br><br>
            <button type="button" ng-click="subStr()">
                Click Me
            </button>
            <p>Result = '{{res}}'</p>
        </div>
    </div>
</body>
  
</html

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller",
            function ($scope) {
                $scope.str = "A Computer Science portal";
                $scope.res = '';
                $scope.subStr = function () {
                    $scope.res = $scope.str.substr(2, 16);
                }
            });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to slice a string using
        substr() method in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            String - '{{str}}'<br><br>
            <button type="button" ng-click="subStr()">
                Click Me
            </button>
            <p>Result = '{{res}}'</p>
        </div>
    </div>
</body>
  
</html>       

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.