JavaScript Course

String Methods split, join, substring

String Methods: split()

The split() method in JavaScript is used to split a string into an array of substrings and return the new array. It takes a separator as its argument, which is the character or string that you want to split the string on.

For example, the following code splits the string "Hello world" on the space character:

const str = "Hello world";
const arr = str.split(" ");
console.log(arr); // Output: ["Hello", "world"]

The split() method can also take a regular expression as its argument. This allows you to split the string on more complex patterns. For example, the following code splits the string "Hello world" on the word boundary:

const str = "Hello world";
const arr = str.split(/\b/);
console.log(arr); // Output: ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]

The split() method is a powerful tool for working with strings. It can be used to extract substrings, tokenize strings, and perform other operations.

Practical Ways to Remember Split()

  • Remember that the split() method takes a separator as its argument.
  • The separator can be a single character or a regular expression.
  • The split() method returns an array of substrings.

Quick Reference

Argument Description
separator The character or regular expression to split the string on
limit The maximum number of substrings to return

Example

const str = "Hello world";
const arr = str.split(" ");

console.log(arr); // Output: ["Hello", "world"]

In this example, the split() method is used to split the string "Hello world" on the space character. The resulting array is stored in the variable arr.

Next Up: String Methods: join()

The join() method is used to join an array of strings into a single string. It takes an optional separator as its argument, which is the character or string that you want to join the strings with.

String Methods: join()

The join() method

combines an array of strings into a single string, separated by a specified delimiter.

Easy Tip: Think of it as a Glue Gun

Just like a glue gun joins pieces of paper, the join() method joins strings. It's like a special glue that sticks strings together.

Practical Ways to Remember join()

  • It's like the reverse of split(): split() breaks a string down into pieces, join() puts them back together.
  • Remember that the separator (delimiter) is like the glue between the strings.

Quick Reference

  • Join takes an array of strings as an argument.
  • It returns a single string.
  • The separator (delimiter) is optional.

Example in JavaScript:

const names = ["John", "Mary", "Bob"];
// Join the names with a comma as the separator
const joinedNames = names.join(", ");
console.log(joinedNames); // Output: "John, Mary, Bob"

Next Up: String Methods: substring()

This method gets a portion of a string from a specified start and end index, creating a new string. Let's explore it next!

String Methods: substring()

Imagine substring() as a highlighter:

Just like a highlighter can highlight a part of a document, the substring() method lets you select and extract a part of a string. You can pick any starting point and any ending point within the string.

How to use substring():

const newString = originalString.substring(startIndex, endIndex);
  • startIndex: The index of the character where you want to start extracting.
  • endIndex: The index of the character where you want to stop extracting. (Note: It's not included in the result.)

Example:

const name = "JavaScript";
const firstThreeLetters = name.substring(0, 3);
console.log(firstThreeLetters); // Output: "Jav"

Quick Tips for Remembering substring():

  • Remember the first three letters: "sub". They tell you it's a sub-part of the original string.
  • Think of it as "stretching a rubber band" from the starting point to the ending point.
Share Button