Details
-
Improvement
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
None
-
None
-
None
Description
public static String unwrap(final String str, final String wrapToken) {
if (isEmpty(str) || isEmpty(wrapToken) || str.length() < 2 * wrapToken.length())
if (startsWith(str, wrapToken) && endsWith(str, wrapToken)) {
// When we come here, startIndex must be 0. Thus, startIndex is redundant.
final int startIndex = str.indexOf(wrapToken);
final int endIndex = str.lastIndexOf(wrapToken);
final int wrapLength = wrapToken.length();
// (startIndex != -1 && endIndex != -1) is always true
if (startIndex != -1 && endIndex != -1) {
return str.substring(startIndex + wrapLength, endIndex);
}
}
return str;
}
As commented above, unwrap() could be improved:
public static String unwrap(final String str, final String wrapToken)
{ if (isEmpty(str) || isEmpty(wrapToken) || str.length() < 2 * wrapToken.length()) \{ return str; } if (startsWith(str, wrapToken) && endsWith(str, wrapToken)) {
return str.substring(wrapToken.length(), str.lastIndexOf(wrapToken));
}
}
return str;
}