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!
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.
SlicePipelinkCreates a new Array or String containing a subset (slice) of the elements.
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.
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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With