Skip to content

Commit

Permalink
Add Strings.substring() that handles short strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewr committed Jan 9, 2014
1 parent 01b18ad commit 9caf302
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/elasticsearch/common/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1548,4 +1548,21 @@ public static String randomBase64UUID(Random random) {
throw new ElasticsearchIllegalStateException("should not be thrown");
}
}

/**
* Return substring(beginIndex, endIndex) that is impervious to string length.
*/
public static String substring(String s, int beginIndex, int endIndex) {
if (s == null) {
return s;
}

int realEndIndex = s.length() > 0 ? s.length() - 1 : 0;

if (endIndex > realEndIndex) {
return s.substring(beginIndex);
} else {
return s.substring(beginIndex, endIndex);
}
}
}

0 comments on commit 9caf302

Please sign in to comment.