Swapna
Swapna

Reputation: 403

how to trim characters in string in angular js

i want to trim the characters from the string . my output string is "Val:980". i need to trim the first 4 letters so that i will get 980 only.

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
        $http.get('https://example.com', {
            headers: {
                'Authorization': 'Basic fakjle=='
            }
        }).then(function(response) {
            $scope.names = response.data;
            $scope.decodedFrame = atob($scope.names.dataFrame) //Val:980               });
        });
    });
</script>

<div ng-app="myApp" ng-controller="myCtrl">
    <table>
        <tr>
            <td> {{decodedFrame} </td>
        </tr>
    </table>
</div>

Upvotes: 1

Views: 2171

Answers (6)

Saleh
Saleh

Reputation: 160

Try:

var value = $scope.decodedFrame.split(":")[1]; // actually safer to use split
value = (parseFloat(value) * 0.23) / 162;

And take a look at (for more string methods): https://www.w3schools.com/js/js_string_methods.asp

Upvotes: 1

Vitalii Andrusishyn
Vitalii Andrusishyn

Reputation: 4182

You actually have many ways. One of them:

$scope.decodedFrame = atob($scope.names.dataFrame);
$scope.decodedFrame = $scope.decodedFrame.substring(4);

But if you have dynamic length of name of this parameters ("Val", "Value", "V",...) you also can try something like this:

var tempValue = atob($scope.names.dataFrame);
tempValue = tempValue.split(":");
tempValue = tempValue[1] || '';
$scope.decodedFrame = tempValue.trim();

Upvotes: 1

flashjpr
flashjpr

Reputation: 470

People answered on how to trim it, but also trim it right in the .then block, so you will have a clear and tidy mark-up.

Best advice will be to create a customFunction that trimms the first 4 characters and apply that function for all your results in the .then successfull callback.

Upvotes: 1

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

You can use substring method to get the characters like this

$scope.decodedFrame.substring(4);

But if you need to get all the characters after : symbol then use the split method

$scope.decodedFrame.split(':')[1];

Upvotes: 1

Nikhilesh K V
Nikhilesh K V

Reputation: 1480

If you are sure that you will always get "Val:" along with the intended number(980), then you can use split in the following way :

$scope.decodedFrame = $scope.decodedFrame.split('Val:')[1];

Upvotes: 1

BX.T
BX.T

Reputation: 11

$scope.decodedFrame.substring(4);

Upvotes: 1

Related Questions