How to slice numbers from last index using angular pipe?

Viewed 4828
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!

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 }}
Related