else if (startPos > endPos) // start after end
return null;
else // both start and end found
return from.substring(startPos, endPos + 1);
}
The method delimitedString returns a new String object containing the string inside from that is
delimited by start and endthat is, it starts with the character start and ends with the character end. If
start is found but not end, the method returns a new String object containing everything from the start
position to the end of the string. The method delimitedString works by using the two overloaded forms
of substring. The first form takes only an initial start position and returns a new string containing
everything in the original string from that point on. The second form takes both a start and an end position and
returns a new string that contains all the characters in the original string from the start to the endpoint,
including the character at the start but not the one at the end. This "up to but not including the end" behavior is
the reason that the method adds one to endPos to include the delimiter characters in the returned string. For
example, the string returned by
delimitedString("Il a dit «Bonjour!»", '«', '»');
is
«Bonjour!»
Here are the rest of the "related string" methods:
public Stringreplace(char oldChar, char newChar)
Returns a String with all instances of oldChar replaced with the
character newChar.
public Stringreplace(CharSequence oldSeq, CharSquence newSeq)
Returns a String with each occurrence of the subsequence oldSeq
replaced by the subsequence newSeq.
public Stringtrim()
Returns a String with leading and trailing whitespace stripped. Whitespace
characters are those identified as such by the
Character.isWhitespace method and include space, tab, and newline.
A number of methods return related strings based on a match with a given regular expressionsee "Regular
Expression Matching" on page 321:
public StringreplaceFirst(String regex, String repStr)
Returns a String with the first substring that matches the regular
expression regex replaced by repStr. Invoked on str, this is equivalent
to
Pattern.compile(regex).matcher(str).replaceFirst(repStr).