Angular 2+ String Helper
Ngx lacks complete string manipulation operations. This is an attempt to fill that gap.
Installation
Install via npm
$ npm i ngx-string-helper --save
API Usage
To use it in your Angular 2 app import the module in your root module(app.module.ts) as shown below.
import { NgxStrHelperModule } from 'ngx-string-helper';
...
...
@NgModule({
imports:[
NgxStrHelperModule
],
})
In your components simply import the service.
import { NgxStrHelper } from 'ngx-string-helper';
...
...
export class YOUR_MODULE {
constructor(private ngxStrHelper: NgxStrHelper) {
}
}
NgxStrHelper.isNullOrEmpty();
let sampleString = "Input is a string";
let isNullOrEmpty = ngxStrHelper.isNullOrEmpty(sampleString);
NgxStrHelper.isBlank();
isBlank("");
isBlank("\n");
isBlank(" ");
isBlank("a");
NgxStrHelper.contains();
if (ngxStrHelper.contains('Input is a string', 'is')) {
}
NgxStrHelper.truncate();
var sampleString = "Input is a string";
var str = ngxStrHelper.truncate(sampleString, 9);
NgxStrHelper.toSlug();
var sampleString = "Input is a string";
var slug = ngxStrHelper.toSlug(sampleString);
NgxStrHelper.decapitalize();
Converts first letter of the string to lowercase.
ngxStrHelper.decapitalize("Foo Bar");
NgxStrHelper.capitalize();
Converts first letter of the string to uppercase. If true is passed as second argument the rest of the string will be converted to lower case.
ngxStrHelper.capitalize("foo Bar");
ngxStrHelper.capitalize("FOO Bar", true);
NgxStrHelper.clean();
Trim and replace multiple spaces with a single space.
ngxStrHelper.clean(" foo bar ");
NgxStrHelper.chars();
ngxStrHelper.chars("Hello");
NgxStrHelper.swapCase();
Returns a copy of the string in which all the case-based characters have had their case swapped.
ngxStrHelper.swapCase("hELLO");
NgxStrHelper.include();
Tests if string contains a substring.
ngxStrHelper.include("foobar", "ob");
NgxStrHelper.count();
Returns number of occurrences of substring in string.
ngxStrHelper.count("Hello world", "l");
NgxStrHelper.escapeHTML();
Converts HTML special characters to their entity equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos.
ngxStrHelper.escapeHTML("<div>Blah blah blah</div>");
NgxStrHelper.unescapeHTML();
Converts entity characters to HTML equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos, nbsp.
ngxStrHelper.unescapeHTML("<div>Blah blah blah</div>");
NgxStrHelper.insert();
ngxStrHelper.insert("Hellworld", 4, "o ");
NgxStrHelper.replaceAll();
ngxStrHelper.replaceAll("foo", "o", "a");
NgxStrHelper.reverse();
Return reversed string
ngxStrHelper.reverse("foobar");
NgxStrHelper.startsWith();
This method checks whether the string begins with starts at position (default: 0)
ngxStrHelper.startsWith("image.gif", "image");
ngxStrHelper.startsWith(".vimrc", "vim", 1);
NgxStrHelper.endsWith();
This method checks whether the string ends with ends at position (default: string.length).
ngxStrHelper.endsWith("image.gif", "gif");
ngxStrHelper.("image.old.gif", "old", 9);
Thanks and enjoy!