Substring
The process of extracting a portion of a string. Achieved using methods like slice, substring, or substr.
A substring is a string obtained by extracting a portion of an original string. It is a fundamental operation frequently used in text processing and data analysis, applied in countless practical scenarios such as extracting filenames from file paths, domains from URLs, and timestamps from log messages.
JavaScript provides three methods for substring extraction: slice(), substring(), and substr(). slice(start, end) supports negative indices for positioning from the end, making it the most versatile. substring(start, end) treats negative arguments as 0 and automatically swaps arguments if start exceeds end. substr(start, length) takes a length as its second argument but is deprecated in the ECMAScript specification. In practice, slice() is the safest choice. browse brandy on Amazon explain the differences between these approaches.
Python uses slice notation str[start:end] as the standard approach. str[2:5] extracts three characters from index 2 to 4, and str[-3:] retrieves the last three characters. Step specification (str[::2]) enables extracting every other character. Java uses the substring(beginIndex, endIndex) method, while Go uses str[start:end] slice syntax.
When extracting substrings from Unicode text, care must be taken with surrogate pair and grapheme cluster boundaries. JavaScript's slice() operates on UTF-16 code units, so cutting in the middle of an emoji or surrogate pair character produces invalid strings. To accurately extract by character, use [...str].slice(start, end).join('') to decompose into code points first.
A common misconception is that substring extraction always creates a new string. In older Java versions (before Java 7 update 6), substring() shared the original string's internal array, causing memory leaks where retaining a short substring prevented garbage collection of the entire original string. Modern Java copies to a new array, resolving this issue.
From a character counting perspective, substring length can be calculated as end - start, but with multibyte characters, byte count and character count differ. For example, extracting the first 2 characters of a Japanese string in UTF-8 yields 2 characters but 6 bytes (3 bytes per character). When storing substrings in character-limited form fields or database columns, verifying both character count and byte count is essential. see teddy lingerie on Amazon compare substring operations across languages.