Advance Angular Lecture 4
Advance Angular Lecture 4
17
4. String Functions - 2
CHECKING FUNCTIONS
Checking functions in string functions in Angular are methods used to verify or validate certain
characteristics or properties of a string. These functions typically return a boolean value indicating whether
a particular condition is true or false for the given string. They help in performing checks or validations on
strings in Angular applications.
HTML—
<div>
<button (click)="checkStartsWith()">Starts With</button>
<button (click)="checkEndsWith()">Ends With</button>
<button (click)="findIndexOf()">Index Of</button>
<button (click)="findLastIndexOf()">Last Index Of</button>
<button (click)="findSearch()">Search</button>
<button (click)="findMatch()">Match</button>
</div>
TS—
original String: string = "Hello, World!";
checkStartsWith():
Checks if the string starts with the specified substring 'Hello'. Returns true if it does, otherwise false.
checkStartsWith() {
// Checks if a string starts with a specified substring
let startsWithHello: boolean = this.originalString.startsWith('Hello');
console.log(startsWithHello); // Output: true
}
Output: true
checkEndsWith():
Checks if the string ends with the specified substring 'World!'. Returns true if it does, otherwise false.
checkEndsWith() {
// Checks if a string ends with a specified substring
let endsWithWorld: boolean = this.originalString.endsWith('World!');
console.log(endsWithWorld); // Output: true
}
Output: true
findIndexOf():
Finds the index of the first occurrence of the specified value ',' in the string. Returns the index if found,
otherwise -1.
1
Advance Angular v.17
4. String Functions - 2
findIndexOf() {
// Returns the index of the first occurrence of the specified value
let indexOfComma: number = this.originalString.indexOf(',');
console.log(indexOfComma); // Output: 5
}
Output: 5
findLastIndexOf():
Finds the index of the last occurrence of the specified value 'l' in the string. Returns the index if found,
otherwise -1.
findLastIndexOf() {
// Returns the index of the last occurrence of the specified value
let lastIndexOfL: number = this.originalString.lastIndexOf('l');
console.log(lastIndexOfL); // Output: 10
}
Output: 10
findSearch():
Searches the string for the specified value 'World'. Returns the position of the first occurrence if found,
otherwise -1.
findSearch() {
// Searches a string for a specified value or regular expression
let searchResult: number = this.originalString.search('World');
console.log(searchResult); // Output: 7
}
Output: 7
findMatch():
Searches the string for a match against the regular expression /lo/g. Returns an array containing all
matches if found, otherwise null.
findMatch() {
// Searches a string for a match against a regular expression
let matchResult: RegExpMatchArray | null =
this.originalString.match(/lo/g);
console.log(matchResult); // Output: ['lo', 'lo']
}