Package edu.cnm.deepdive
Class StringWarp
java.lang.Object
edu.cnm.deepdive.StringWarp
public class StringWarp extends Object
Defines the static method
substring(String, int, int)
, which computes—and
optionally reverses—a substring of a specified string. Implementation of this method is
included in the assignments, extra credit, or practical exam problems of the Deep Dive Coding
Java training programs.-
Method Summary
-
Method Details
-
substring
public static String substring(String input, int beginIndex, int endIndex) throws IndexOutOfBoundsExceptionExtracts and returns a substring of the input string, reversing the string whenbeginIndex > endIndex
. That is,StringWarp.substring(input, beginIndex, fromIndex)
returns the same result asinput.substring(beginIndex, endIndex)
when(beginIndex <= endIndex)
, but returns the reverse ofinput.substring(endIndex, beginIndex)
when(beginIndex > endIndex)
.For example,
StringWarp.substring("hello", 1, 4)
returns the string"ell"
, whileStringWarp.substring("hello", 4, 1)
returns"lle"
.If the larger of
beginIndex
andendIndex
exceedsinput.length()
, or the smaller of the two is less than0
,StringIndexOutOfBoundsException
will be thrown.- Parameters:
input
- Source string.beginIndex
- Inclusive starting position (or exclusive ending position, if(beginIndex > endIndex)
).endIndex
- Exclusive ending position (or inclusive starting position, if(beginIndex > endIndex)
).- Returns:
- Extracted substring.
- Throws:
IndexOutOfBoundsException
- WhenbeginIndex
orendIndex
are out of bounds for the specified substring operation.
-