What is the difference between substr and substring?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In computer programming, string manipulation is a critical operation and different languages offer various built-in methods to handle such operations. Among these, substr and substring are two common methods used to extract a portion of a string. Despite performing similar functions, these methods have distinctions in how they are implemented across different programming languages and even within the same language. Here we'll explore how substr and substring differ conceptually, as well as provide practical examples.
Understanding substr
substr is a method used to obtain a substring from a string, typically starting at a given index and extending for a specified number of characters or until the end of the string if the length is not specified. The general syntax can be understood as follows:
- start: The initial index of the substring. The index of the first character in a string is usually 0.
- length (optional): The number of characters to include in the returned substring.
Example in JavaScript:
In the example above, substr(7, 5) extracts "World" starting from index 7 (inclusive) pulling out 5 characters.
Understanding substring
substring is another string method that extracts a segment of a string between two specified indices: the start index and the end index, where the character at the end index is not included. The syntax generally looks like:
- startIndex: The position where to start the extraction.
- endIndex: The position up to, but not including, which characters are to be extracted.
Example in JavaScript:
Here, substring(7, 12) starts at index 7 and ends at index 12, not including the character at index 12, so the returned string is "World".
Key Differences
While both substr and substring serve to extract parts of strings, they differ in their parameters and how they handle negative or unexpected values:
- Parameter Differences: In
substr, the second parameter indicates the number of characters to extract, while insubstring, the second parameter specifies the upper boundary index (non-inclusive) of the extraction. - Negative Values:
substrinterprets a negative start index as starting from the end of the string backwards, whilesubstringtreats a negative index as0. - Handling of Indices: With
substring, ifstartIndexis greater thanendIndex, the method swaps the two values. In contrast,substrdoes not swap and merely counts forward the specified length.
Usage Recommendations
Choosing between substr and substring can depend on specific needs:
- Use
substrwhen you know the length of the substring but not the end index. - Opt for
substringwhen you are sure about the exact start and end indices of the substring.
Summary Table
Here’s a quick comparison chart:
| Feature | substr | substring |
| Parameters | start index, length of substring | start index, end index |
| Negative start index | Interpreted as from end backwards | Treated as 0 |
| If start > end | Pulls length characters | Swaps indexes |
Conclusion
In conclusion, though substr and substring might seem similar, understanding their differences is key to using them effectively. The choice between the two would depend on specific requirements and nuances of the programming language in use. Consider the details and examples provided above to make an appropriate choice when working with string manipulations.

