Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice numbers from last index using angular pipe?

import { Component } from '@angular/core';
@Component({
  selector: 'string-app',
  template: `
           <h3>String Example</h3>
       {{myStr | slice:3:7}} <br/>
       {{myStr | slice:3:-2}} <br/>
       {{myStr | slice:6}} <br/>
       {{myStr | slice:-6}} <br/>          
         ` 
})
export class StringSlicePipeComponent {
    myStr: string = "abcdefghijk";
} 

output.
    String Example
    defg
    defghi
    ghijk
    fghijk 

In my case, my string will be always ending with 3 digits.How to slice those 3 digits

Example

my string : abcdef123, ahdvvhvhv456 

i want to slice 123 and 456 from the end. How to do this?

Any help would be appreciated!

like image 524
RS17 Avatar asked Sep 07 '18 18:09

RS17


People also ask

What is the output if the value for end is omitted in SlicePipe?

end : The ending index of the subset to return. omitted: return all items until the end. if positive: return all items before end index of the list or string.

What is SlicePipe in angular?

SlicePipelinkCreates a new Array or String containing a subset (slice) of the elements.

How do you use custom pipes?

Steps Involved In Creating a Custom Pipe In Angular are: 1) Create a Pipe Class and decorate it with the decorator @Pipe. 2) Supply a name property to be used as template code name. 3) Register your Pipe in the module under declarations. 4) Finally, implement PipeTransform and write transformation logic.


1 Answers

To remove the last 3 characters from the string, you can use:

{{ myStr | slice:0:-3 }}

See this stackblitz for a demo.


On the other hand, if you want to show only the last 3 characters of the string, you can use:

{{ myStr | slice:-3 }}
like image 112
ConnorsFan Avatar answered Sep 28 '22 06:09

ConnorsFan