Skip to content

Instantly share code, notes, and snippets.

@mathcodes
Created June 26, 2023 15:05
Show Gist options
  • Save mathcodes/bc5f3c089a8726e37fa28e34571350d8 to your computer and use it in GitHub Desktop.
Save mathcodes/bc5f3c089a8726e37fa28e34571350d8 to your computer and use it in GitHub Desktop.
String Methods

String Methods Cheatsheet

Table of Contents

String Length

  • str.length: Returns the length of the string.

Case Conversion

  • str.toUpperCase(): Converts the string to uppercase.

    • Parameters: None.
    • Example:
      const str = 'hello';
      console.log(str.toUpperCase()); // Output: HELLO
  • str.toLowerCase(): Converts the string to lowercase.

    • Parameters: None.
    • Example:
      const str = 'WORLD';
      console.log(str.toLowerCase()); // Output: world

Substring Extraction

  • str.slice(startIndex, endIndex): Extracts a portion of the string from startIndex to endIndex (exclusive).

    • Parameters:
      • startIndex: The starting index of the substring (inclusive).
      • endIndex (optional): The ending index of the substring (exclusive).
    • Example:
      const str = 'Hello, World!';
      console.log(str.slice(7, 12)); // Output: World
  • str.substring(startIndex, endIndex): Extracts a portion of the string from startIndex to endIndex (exclusive), but treats negative indices as 0.

    • Parameters:
      • startIndex: The starting index of the substring (inclusive).
      • endIndex (optional): The ending index of the substring (exclusive).
    • Example:
      const str = 'Hello, World!';
      console.log(str.substring(7, 12)); // Output: World
  • str.substr(startIndex, length): Extracts a portion of the string starting from startIndex with the specified length.

    • Parameters:
      • startIndex: The starting index of the substring (inclusive).
      • length (optional): The length of the substring.
    • Example:
      const str = 'Hello, World!';
      console.log(str.substr(7, 5)); // Output: World

String Manipulation

  • str.concat(str2, str3, ..., strN): Concatenates one or more strings with the original string.

    • Parameters:
      • str2, str3, ..., strN (optional): Strings to concatenate.
    • Example:
      const str1 = 'Hello';
      const str2 = 'World';
      console.log(str1.concat(', ', str2)); // Output: Hello, World
  • str.replace(searchValue, replaceValue): Replaces occurrences of searchValue with replaceValue.

    • Parameters:
      • searchValue: The value to search for.
      • replaceValue: The value to replace occurrences of searchValue.
    • Example:
      const str = 'Hello, World!';
      console.log(str.replace('World', 'John')); // Output: Hello, John!
  • str.trim(): Removes leading and trailing whitespace from the string.

    • Parameters: None.
    • Example:
      const str = '   Hello, World!   ';
      console.log(str.trim()); // Output: Hello, World!
  • str.split(separator): Splits the string into an array of substrings based on the separator character.

    • Parameters:
      • separator: The character or substring used to determine where to split the string.
    • Example:
      const str = 'Hello,World,!';
      console.log(str.split(',')); // Output: ['Hello', 'World', '!']
  • str.charAt(index): Returns the character at the specified index.

    • Parameters:
      • index: The index of the character to retrieve.
    • Example:
      const str = 'Hello, World!';
      console.log(str.charAt(7)); // Output: W
  • str.charCodeAt(index): Returns the Unicode value of the character at the specified index.

    • Parameters:
      • index: The index of the character.
    • Example:
      const str = 'Hello, World!';
      console.log(str.charCodeAt(7)); // Output: 87
  • str.indexOf(searchValue, startIndex): Returns the index of the first occurrence of searchValue starting from startIndex.

    • Parameters:
      • searchValue: The value to search for.
      • startIndex (optional): The index to start searching from.
    • Example:
      const str = 'Hello, World!';
      console.log(str.indexOf('o')); // Output: 4
  • str.lastIndexOf(searchValue, startIndex): Returns the index of the last occurrence of searchValue starting from startIndex.

    • Parameters:
      • searchValue: The value to search for.
      • startIndex (optional): The index to start searching from.
    • Example:
      const str = 'Hello, World!';
      console.log(str.lastIndexOf('o')); // Output: 8
  • str.startsWith(searchValue): Checks if the string starts with searchValue.

    • Parameters:
      • searchValue: The value to search for.
    • Example:
      const str = 'Hello, World!';
      console.log(str.startsWith('Hello')); // Output: true
  • str.endsWith(searchValue): Checks if the string ends with searchValue.

    • Parameters:
      • searchValue: The value to search for.
    • Example:
      const str = 'Hello, World!';
      console.log(str.endsWith('World!')); // Output: true
  • str.includes(searchValue): Checks if the string contains searchValue.

    • Parameters:
      • searchValue: The value to search for.
    • Example:
      const str = 'Hello, World!';
      console.log(str.includes('World')); // Output: true

String Formatting

  • str.padStart(targetLength, padString): Pads the string from the start with padString until it reaches the targetLength.

    • Parameters:
      • targetLength: The length the string should reach.
      • padString (optional): The string to pad with (default is a space).
    • Example:
      const str = 'Hello';
      console.log(str.padStart(10, 'X')); // Output: XXXXHello
  • str.padEnd(targetLength, padString): Pads the string from the end with padString until it reaches the targetLength.

    • Parameters:
      • targetLength: The length the string should reach.
      • padString (optional): The string to pad with (default is a space).
    • Example:
      const str = 'Hello';
      console.log(str.padEnd(10, 'X')); // Output: HelloXXXX
  • str.repeat(count): Repeats the string count number of times.

    • Parameters:
      • count: The number of times to repeat the string.
    • Example:
      const str = 'Hello';
      console.log(str.repeat(3)); // Output: HelloHelloHello

Template Literal (ES6)

  • ${variable}: Interpolates the value of variable within a string.
  • Multiline strings: Enclose multiline strings using backticks (`).

For more detailed information and additional string methods, please refer to the documentation of the programming language or framework you are using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment