Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove char from string with angular

I'm trying to remove a period '.' from a value that comes from a feed, however I don't really want to do this in my app.js, rather in my view.

So if I do the following:

 value: {{item.v_value}}

I get 3.5, I'd simply like to strip out and render out 35 instead.

So basically reusing the replace function - but on the item value only.

like image 238
Poiro Avatar asked Jun 22 '15 13:06

Poiro


People also ask

How do I remove a character from a string in typescript?

The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with.

How do I remove a substring from a string in typescript?

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.

How to remove the first char of a string?

So, if you want to remove the first character of String, just create a substring from the second character. Since index starts from zero in String, "text". substring(1) is equivalent to deleting the first character.


2 Answers

If you need it to be reusable you can use a filter.

myApp.filter('removeString', function () {
    return function (text) {
        var str = text.replace('thestringtoremove', '');
        return str;
    };
});

Then in your HTML you can something like this:

value: {{item.v_value | removeString}}
like image 146
area28 Avatar answered Oct 19 '22 22:10

area28


Just use replace:

If v_value is a string:

value: {{item.v_value.replace('.', '')}}

If v_value is a number, "cast" it to a string first:

value: {{(item.v_value + '').replace('.', '')}}

Basically, you can use JavaScript in those brackets.

like image 40
Cerbrus Avatar answered Oct 19 '22 21:10

Cerbrus