Advance Angular Lecture 3
Advance Angular Lecture 3
17
3. String Functions - 1
MANIPULATING FUNCTIONS
Manipulating functions in string functions in Angular typically refers to the process of working with string
values within Angular components or services. String manipulation functions in Angular can involve tasks
such as concatenating strings, splitting strings, extracting substrings, replacing parts of strings, converting
strings to different cases (e.g., uppercase, lowercase), and more.
HTML—
<div>
<button (click)="splitString()">Split</button>
<button (click)="replaceString()">Replace</button>
<button (click)="repeatString()">Repeat</button>
<button (click)="concatenateStrings()">Concatenate</button>
<button (click)="trimString()">Trim</button>
<button (click)="toUpperCase()">To Uppercase</button>
<button (click)="toLowerCase()">To Lowercase</button>
<button (click)="getSubstring()">Substring</button>
<button (click)="getSubstr()">Substr</button>
<button (click)="getCharAt()">Char At</button>
</div>
TS—
original String: string = "Hello, World!";
splitString():
Splits the original string into an array of substrings based on the specified separator (,). Each substring is
stored as an element in the resulting array.
splitString() {
// Splits the string into an array of substrings based on a specified
separator
let substrings: string[] = this.originalString.split(',');
console.log(substrings);
}
1
Advance Angular v.17
3. String Functions - 1
concatenateStrings():
Concatenates the original string with the specified string ' Have a nice day!' and returns the combined
string.
concatenateStrings() {
// Concatenates two or more strings and returns a new string
let concatenatedString: string = this.originalString.concat(' Have a
nice day!');
console.log(concatenatedString);
}
trimString():
Removes any leading and trailing whitespace characters from the original string and returns the trimmed
string.
trimString() {
// Removes whitespace from both ends of a string
let trimmedString: string = this.originalString.trim();
console.log(trimmedString);
}
toUpperCase():
Converts all characters in the original string to uppercase letters and returns the modified string.
toUpperCase() {
// Converts a string to uppercase letters
let upperCaseString: string = this.originalString.toUpperCase();
console.log(upperCaseString);
}
2
Advance Angular v.17
3. String Functions - 1
toLowerCase():
Converts all characters in the original string to lowercase letters and returns the modified string.
toLowerCase() {
// Converts a string to lowercase letters
let lowerCaseString: string = this.originalString.toLowerCase();
console.log(lowerCaseString);
}
getSubstring():
Extracts a substring from the original string starting at index 7 and ending at index 12 (exclusive) and
returns the extracted substring.
getSubstring() {
// Returns a substring of a string between two specified indices
let substring: string = this.originalString.substring(7, 12);
console.log(substring);
}
Output: World
getSubstr():
Retrieves a substring from the original string starting at index 7 with a length of 5 characters and returns
the extracted substring.
getSubstr() {
// Returns a portion of a string starting from a specified index to the
end of the string, or to a specified length
let substr: string = this.originalString.substr(7, 5);
console.log(substr);
}
Output: World
getCharAt(): Retrieves the character at index 7 in the original string and returns it.
getCharAt() {
// Returns the character at a specified index in a string
let char: string = this.originalString.charAt(7);
console.log(char);
}
Output: W