<!-- !
String Methods -->
1. toLowerCase()
Converts a string to lowercase letters.
let str = "Hello String In JS";
let result = str.toLowerCase();
console.log(result); // Output: "hello string in js"
2 . toUpperCase()
Converts a string to uppercase letters.
let str = "Hello String In Js";
let result = str.toUpperCase();
console.log(result); // Output: "HELLO STRING IN JS"
3. includes()
Checks if a string contains specified characters.
let str = "Hello";
let result = str.includes("ell");
console.log(result); // Output: true
4. startsWith()
Checks if a string starts with specified characters.
let str = "Hello";
let result = str.startsWith("He");
console.log(result); // Output: true
5. endsWith()
Checks if a string ends with specified characters.
let str = "Hello";
let result = str.endsWith("lo");
console.log(result); // Output: true
6. charAt()
Returns the character at a specified index.
let str = "Hello";
let char = str.charAt(1);
console.log(char); // Output: "e"
7. indexOf()
Returns the index of the first occurrence of a specified value.
let str = "Hello";
let index = str.indexOf("l");
console.log(index); // Output: 2
8. lastIndexOf()
Returns the index of the last occurrence of a specified value.
let str = "Hello";
let index = str.lastIndexOf("l");
console.log(index); // Output: 3
9. concat()
Joins two or more strings.
let str1 = "Hello";
let str2 = "JavaScript";
let result = str1.concat(" ", str2);
console.log(result); // Output: "Hello JavaScript"
10. split()
Splits a string into an array of substrings.
let str = "Hello javascript";
let result = str.split(" ");
console.log(result); // Output: [ 'Hello', 'javascript' ]
11. trim()
Removes whitespace from both ends of a string.
let str = " Hello World ";
let result = str.trim();
console.log(result); // Output: "Hello World"
12. trimStart()
Removes whitespace from the beginning of a string.
let str = " Hello World ";
let result = str.trimStart();
console.log(result); // Output: "Hello World "
13. trimEnd()
Removes whitespace from the end of a string.
let str = " Hello World ";
let result = str.trimEnd();
console.log(result); // Output: " Hello World"
14. slice()
The slice() method extracts a part of a string and returns it as a new string,
without modifying the original string.
# Syntax
string.slice(startIndex, endIndex)
- start: The position where to start the extraction. If negative, it starts
counting from the end of the string.
- end: The position where to end the extraction. If omitted, it extracts to
the end of the string. The character at this index will not be included.
15. substring()
The substring() method extracts characters from a string, between two
specified indices, and returns the new substring. The substring() method swaps
the two arguments if start is greater than end.
# Syntax
string.substring(start, end)
Key Differences
1. **Handling of Negative Indices**:
- slice(): If start or end is negative, it counts from the end of the
string.
- substring(): Treats negative values as 0.
2. **Parameter Swapping**:
- substring(start, end): Swaps the two arguments if start is greater
than end.
- slice(start, end) and substr(start, length): Do not swap the
arguments.
16. replace()
Searches a string for a specified value, or a regular expression, and returns
a new string where the specified values are replaced.
let str = "Hello world";
let result = str.replace("world", "there");
console.log(result); // Output: "Hello there"
17. replaceAll()
this method is used to return a new string with all matches of a pattern
replaced by a replacement
let str = "Hello world";
let result = str.replaceAll("l", "r");
console.log(result); // Output: "Herro worrd"
<!--! What is String Interpolation? -->
String interpolation is a method of embedding variables or expressions within
a string.
Instead of concatenating strings with variables using +, string interpolation
allows for cleaner and more readable code.
It is achieved using template literals, which are enclosed by backticks ( ).
let name = "VK";
let age = 25;
let message = My name is ${name} and I am ${age} years old.;
console.log(message);
<!--? Output: My name is VK and I am 25 years old. -->
// ! declaration of String
//! 1. by using literals
let str1 = "hello"
let str2 = 'hello'
let str3 = HELLO
// ! 2. by using object
let strObj1 = new String("hello")
let strObj2 = new String("hello")
console.log(str1)
console.log(strObj1)
console.log(str1 == str2) // true
console.log(strObj1 == strObj2) // false
// ! String Methods
// ! how to know the length of the string
console.log(str1.length)
// ! 1. toUpperCase()
let str4 = 'hello'
let upper = str4.toUpperCase()
console.log(str4); // hello
console.log(upper) //HELLO
//! 2. toLowerCase()
let str5 = "HELLO"
let lower = str5.toLowerCase()
console.log(str5) // HELLO
console.log(lower) // hello
//! 3. charAt()
let str6 = 'hello how are you lady ? '
console.log(str6.charAt(2)) // l
// ! 4. indexOf()
console.log(str6.indexOf('l')) // 2
// ! 5. lastIndexOf()
console.log(str6.lastIndexOf('l')) // 18
// ! 6. concat()
let str7 = 'hello'
let str8 = 'how are you'
let combined = str7.concat(" ", str8, " Kaif")
console.log(combined)
// ! 7. trim()
let str9 = " hi "
console.log(str9.length) // 7
console.log(str9.trim()) // it will remove the space from both the sides ..
console.log(str9.trim().length) // 2
// ! 8. trimStart()
console.log(str9.trimStart()) // removes the space from starting
console.log(str9.trimStart().length) //5
// ! 9. trimEnd()
console.log(str9.trimEnd()) // removes the space from the ending
console.log(str9.trimEnd().length) // 4
// ! 10. includes()
let str10 = 'hello how are you'
console.log(str10.includes('how'))
//! 11. startsWith()
let str11 = 'my name is arun'
console.log(str11.startsWith('my '))
// ! 12. endsWith()
console.log(str11.endsWith('un'))
// ! 13. split()
let str12 = 'hello how are you'
let arr = str12.split(" ")
console.log(arr)
// ! 14. slice()
console.log(str12.slice(1,7)) // from 1 to 6 index (ello h)
console.log(str12.slice(1)) // from 1 to everything (ello how are you)
console.log(str12.slice(-3)) // you
console.log(str12.slice(-3,-1)) // yo
console.log(str12.slice(6,1)) // it will not give output
// ! 15. substring()
console.log(str12.substring(1,7))
console.log(str12.substring(1))
console.log(str12.substring(-8)) // it will convert -8 to 0
console.log(str12.substring(6,1)) // ello (it will swap 6,1 to 1,6)
// ! String Interpolation
let myName = "rohit"
console.log(my name is ${myName});