1

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>
1
  • 1
    substr or substring should do the job. Or you could go for split or a regex if you're not actually sure about the length of the prefix. Commented Feb 17, 2017 at 10:28

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

can u tell me how to do mathematical operation.(decodedFrame*0.23) / 162
Can you give the exact error, it looks like you are not using $scope.decodedFrame, are you putting the code in html because I don't think this will work in html. You need to calculate it and then read the scope value in html by angular.
$scope.Value = (parseFloat(decodedFrame) * 0.23) / 162; decodedFrame is undefined
decodedFrame, is a scope property, decodedFrame doesn't exist outside the scope object. This should work (parseFloat($scope.decodedFrame) * 0.23) / 162;
1

$scope.decodedFrame.substring(4);

Comments

1

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];

1 Comment

can u tell me how to do mathematical operation.(decodedFrame*0.23) / 162
1

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];

Comments

1

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.

Comments

1

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.