Java Reference
In-Depth Information
if (startPos == -1) // no start found
return null;
else if (endPos == -1) // no end found
return from.substring(startPos);
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
end
that 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: