Programming
String Manipulation
Coding Concepts
JavaScript Methods
Substr vs Substring

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:

javascript
str.substr(start[, length])
  • 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:

javascript
let text = "Hello, World!";
let sub = text.substr(7, 5); // 'World'

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:

javascript
str.substring(startIndex, endIndex)
  • 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:

javascript
let text = "Hello, World!";
let segment = text.substring(7, 12); // 'World'

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:

  1. Parameter Differences: In substr, the second parameter indicates the number of characters to extract, while in substring, the second parameter specifies the upper boundary index (non-inclusive) of the extraction.
  2. Negative Values: substr interprets a negative start index as starting from the end of the string backwards, while substring treats a negative index as 0.
  3. Handling of Indices: With substring, if startIndex is greater than endIndex, the method swaps the two values. In contrast, substr does not swap and merely counts forward the specified length.

Usage Recommendations

Choosing between substr and substring can depend on specific needs:

  • Use substr when you know the length of the substring but not the end index.
  • Opt for substring when you are sure about the exact start and end indices of the substring.

Summary Table

Here’s a quick comparison chart:

Featuresubstrsubstring
Parametersstart index, length of substringstart index, end index
Negative start indexInterpreted as from end backwardsTreated as 0
If start > endPulls length charactersSwaps 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.


Course illustration
Course illustration

All Rights Reserved.