<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JavaScript String Splitting: substring() vs. split()</title> </head> <body> <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"
위 내용은 JavaScript에서 문자열을 하위 문자로 분할하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!