String Methods πŸš€πŸš€

Β·

4 min read

String Methods πŸš€πŸš€

Before delving into string methods, it's crucial to understand a fundamental aspect of strings: they are immutable. Now, you might be wondering what I mean by 'immutable,' so let me explain πŸ™‚

strings are immutable. Now you guys were thinking about what I mean by immutable, so let me explain this.

Strings are Immutable πŸš€

strings are immutable. This means that once a string is created, its value cannot be changed. Any operation that appears to modify a string actually creates a new string with the modified value.

let str = "Hello, World!";

// Attempt to modify the string
// This doesn't change the original string, it creates a new one

// string.replace(searchValue, replaceValue);
let modifiedStr = str.replace("Hello", "Hi");

console.log(str);          // Output: Hello, World!
console.log(modifiedStr);  // Output: Hi, World!

the replace method is used to replace a specified substring or pattern in a string with another substring.

In this example, the replace method doesn't change the value of the original str variable. Instead, it creates a new string (modifiedstr) with the modified value. The original string remains unchanged.

String MethodsπŸš€

  1. trim() method:- In JavaScript, the trim method is used to remove the leading and trailing whitespace from a string.

     let s = "      Hello, World!      ";
     let trimmedS = s.trim();
     console.log(trimmedS);  // Output: Hello, World!
    

    As strings are immutable, it does not change the actual string, the trim method returns a new string (trimmed string).

  2. toUpperCase() method:- The toUpperCase method is used to convert all the characters in a string to uppercase.

     let str = "Hello, World!";
     let upperCaseStr = str.toUpperCase();
    
     console.log(upperCaseStr);  // Output: HELLO, WORLD!
    

    the toUpperCase method is called on the str variable, which contains the string "Hello, World!". toUpperCase method does not modify the original string; it returns a new string with the changes.

  3. toLowerCase():- The toLowerCase method is used to convert all the characters in a string to lowercase.

     let str = "Hello, World!";
     let lowerCaseStr = str.toLowerCase();
    
     console.log(lowerCaseStr);  // Output: hello, world!
    

    Similar to toUpperCase, the toLowerCase method does not modify the original string; it returns a new string with all characters converted to lowercase.

    If you want to modify the original string, you would need to assign the result back to the variable.

     let str = "Hello, World!";
     str = str.toLowerCase();
    
     console.log(str);  // Output: hello, world!
    

    This way, the original string str is updated with its lowercase version.

  4. slice() method:- The slice method is used to extract a portion of a string and create a new string without modifying the original one. Here is the syntax-

     string.slice(startIndex, endIndex+1);
    
  • startIndex: The index at which to begin extraction. If negative, it is treated as counting from the end of the string.

  • endIndex: The index at which to end extraction.

let str = "Hello, World!";

// Example 1: Extract from index 0 to 4 (so we will take 4+1 index, 
//so that 5th index character will include )

let slicedStr1 = str.slice(0, 5);
console.log(slicedStr1);  // Output: Hello

// Example 2: Extract from index 7 to the end.
let slicedStr2 = str.slice(7);  // ignored the endIndex here, 
//so the endIndex goes to the end. 
console.log(slicedStr2);  // Output: World!

// Example 3: Extract the last 6 characters
let slicedStr3 = str.slice(-6);
console.log(slicedStr3);  // Output: World!

// Example 4: Extract from index -5 to -1 (not including -1)
let slicedStr4 = str.slice(-5, -1);
console.log(slicedStr4);  // Output: orld

//Example 5: Extract from starting to the end .
let slicedStr5 = str.slice(,);

In these examples, the slice method is used to extract substrings from the original string str based on the specified indices. Remember that slice does not modify the original string; it returns a new string with the extracted portion.

πŸ‘‰In the next episode, we will discuss template strings and string concatenation. I hope you are as excited as I am about delivering the upcoming episodes.

πŸ‘‰If you have any questions, feel free to ask in the comment section, and subscribe to my newsletter. This way, you'll be the first to receive updates when the new episode is released.

If you like my work, you can buy me a coffee and share your thoughts buymeacoffee.com/yashika227x .

Β