Home >Web Front-end >JS Tutorial >How to Split a String into Substrings in JavaScript

How to Split a String into Substrings in JavaScript

William Shakespeare
William ShakespeareOriginal
2025-02-09 10:59:10202browse
<code class="language-html"><!DOCTYPE html>


    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript String Splitting: substring() vs. split()</title>


    <h1>JavaScript String Splitting: `substring()` vs. `split()`</h1>

    <p>JavaScript offers two primary methods for dividing strings into substrings:  `substring()` and `split()`.  Each serves a distinct purpose.</p>

    <h2>`substring()`</h2>
    <p>The `substring()` method extracts a portion of a string based on starting and ending indices. It's ideal for retrieving a single substring from a known position within the string.</p>

    <p><strong>Syntax:</strong> <code>string.substring(startIndex, endIndex)</code></p>
    <ul>
        <li>
<code>startIndex</code>: The index of the first character to include (0-based).</li>
        <li>
<code>endIndex</code> (optional): The index *after* the last character to include. If omitted, the substring extends to the end of the string.</li>
    </ul>

    <p><strong>Example:</strong></p>
    <pre class="brush:php;toolbar:false"><code>
const str = "Hello, world!";
const sub = str.substring(7, 12); // Extracts "world"
console.log(sub); // Output: "world"
    </code>

Often, you'll use `indexOf()` to dynamically determine the start or end indices.

`split()`

The `split()` method divides a string into an array of substrings based on a specified separator. This is perfect for handling strings containing lists of items delimited by a character (e.g., comma-separated values).

Syntax: string.split(separator, limit)

  • separator: The character or string used to divide the string.
  • limit (optional): The maximum number of substrings to return.

Example:

<code>
const csv = "apple,banana,cherry";
const fruits = csv.split(","); // Splits at each comma
console.log(fruits); // Output: ["apple", "banana", "cherry"]
    </code>

Key Differences

Feature `substring()` `split()`
Output Substring (string) Array of substrings
Purpose Extract a portion at specific indices Split into multiple substrings based on a separator
Index-based Yes No (separator-based)

When to Use Each Method

  • Use `substring()` when you need to extract a single, continuous piece of text from a known position within the string.
  • Use `split()` when you need to break a string into multiple parts based on a delimiter, such as commas, spaces, or other characters.
How to Split a String into Substrings in JavaScript

The above is the detailed content of How to Split a String into Substrings in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn