2

I want to search a string using AngularJS by last character of the string. I have done search with first letter. I tried lastIndexOf() and endswith, but I can't get the result.

This one is working fine and its for first letter searching in a string .

var app = angular.module('filterSample', []);
app.controller('filterCtrl', function($scope) {

  $scope.data = {
    messages: ['first', 'second', '3rd', 'fourth', 'fifth']
  }

  $scope.startsWith = function(actual, expected) {
    var lowerStr = (actual + "").toLowerCase();
    return lowerStr.indexOf(expected.toLowerCase()) === 0;
  }
});
<!doctype html>
<html ng-app="filterSample">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js">
  </script>
  <script src="example.js"></script>
</head>

<body>
  <div ng-controller="filterCtrl">
    <input type="text" ng-model="search" />
    <ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
      <li>{{msg}}</li>
    </ul>
  </div>
</body>

</html>
Mistalis
  • 17,793
  • 13
  • 73
  • 97
  • 1
    Use build-in function [String.prototype.endsWith()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) and [String.prototype.startsWith()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) – Satpal Aug 28 '17 at 09:23
  • Possible duplicate of [How can I get last characters of a string using JavaScript](https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string-using-javascript) – Tim Biegeleisen Aug 28 '17 at 09:23
  • thanks Satpal and Tim Biegeleisen – Elamparithi doodleblue Aug 29 '17 at 11:55

3 Answers3

1

You can use the endsWith() method to test the last character of a string:

endsWith() determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

var app = angular.module('filterSample', []);
app.controller('filterCtrl', function ($scope) {

    $scope.data = {
        messages: ['first', 'second', '3rd', 'fourth', 'fifth']
    }

    $scope.startsWith = function (actual, expected) {
        var lowerStr = (actual + "").toLowerCase();
        return lowerStr.endsWith(expected.toLowerCase());
    } 
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>

<div ng-app="filterSample" ng-controller="filterCtrl">
  <input type="text" ng-model="search" placeholder="Search by last char" />
    <ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
      <li>{{msg}}</li>
    </ul>
</div>
Mistalis
  • 17,793
  • 13
  • 73
  • 97
0

just change what index you are looking for

$scope.endsWith = function (actual, expected) {
  var lowerStr = (actual + "").toLowerCase();
  return lowerStr.indexOf(expected.toLowerCase()) === (lowerStr.length - 1);
}
insider
  • 352
  • 2
  • 5
0

You can use charAt

var lastChar = myString.charAt(myString.length-1);
Jayanti Lal
  • 1,175
  • 6
  • 18