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 Details

    • substring

      public static String substring​(String input, int beginIndex, int endIndex) throws IndexOutOfBoundsException
      Extracts and returns a substring of the input string, reversing the string when beginIndex > endIndex. That is, StringWarp.substring(input, beginIndex, fromIndex) returns the same result as input.substring(beginIndex, endIndex) when (beginIndex <= endIndex), but returns the reverse of input.substring(endIndex, beginIndex) when (beginIndex > endIndex).

      For example, StringWarp.substring("hello", 1, 4) returns the string "ell", while StringWarp.substring("hello", 4, 1) returns "lle".

      If the larger of beginIndex and endIndex exceeds input.length(), or the smaller of the two is less than 0, 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 - When beginIndex or endIndex are out of bounds for the specified substring operation.