toUpperCase(): Converts a string to uppercase.
HTML
<template>
<p>{message}</p>
<button onclick={handleUppercase}>Click Me</button>
</template>
Java Script
import { LightningElement } from 'lwc';
export default class StringMethodsExample extends LightningElement {
message= 'I am Learning SALESFORCE';
handleUppercase() {
this.message= this.message.toUpperCase();
}
}
Preview
I AM LEARNING SALESFORCE
Video
Video does not exists.
toLowerCase(): Converts a string to lowercase.
HTML
<template>
<p>{message}</p>
<button onclick={handleLowercase}>Click Me</button>
</template>
Java Script
import { LightningElement } from 'lwc';
export default class StringMethodsExample extends LightningElement {
message= 'I am Learning SALESFORCE';
handleLowercase() {
this.message= this.message.toLowerCase();
}
}
Preview
i am learning salesforce
Video
Video does not exists.
length: Returns the number of characters in a string.
HTML
<template>
<p>The length of the {str} is {strLength}</p>
<button onclick={handleLength}>Click Me</button>
</template>
Java Script
import { LightningElement } from 'lwc';
export default class StringMethodsExample extends LightningElement {
str= 'I am Learning SALESFORCE';
strLength;
handleLength() {
this.strLength = this.str.length();
}
}
Preview
I AM LEARNING SALESFORCE
Video
Video does not exists.
charAt(index): Returns the character at the specified index in a string.
let str = 'Hello';
console.log(str.charAt(0)); // Output: H
concat(str1, str2, ...): Concatenates two or more strings together.
let str1 = 'Hello';
let str2 = 'World';
console.log(str1.concat(', ', str2)); // Output: Hello, World
indexOf(substring): Returns the index of the first occurrence of a specified substring within a string. Returns -1 if the substring is not found.
let str = 'Hello, world!';
console.log(str.indexOf('world')); // Output: 7
substring(startIndex, endIndex): Extracts a portion of a string starting from the startIndex and up to, but not including, the endIndex.
let str = 'Hello, world!';
console.log(str.substring(7, 12)); // Output: world
split(separator): Splits a string into an array of substrings based on a specified separator.
let str = 'apple,banana,orange';
console.log(str.split(',')); // Output: ["apple", "banana", "orange"]
trim(): Removes whitespace (spaces, tabs, or newlines) from the beginning and end of a string.
let str = ' Hello, world! ';
console.log(str.trim()); // Output: Hello, world!
These are just a few examples of the many string methods available in JavaScript. String methods allow you to perform various operations such as manipulating, searching, extracting, and transforming strings to suit your needs.
Mastered by 12 users.