Java Utililty Methods String Sub String

List of utility methods to do String Sub String

Description

The list of methods to do String Sub String are organized into topic(s).

Method

Stringsubstring(String _text, int _idx)
substring
if (_idx >= 0) {
    if (_idx < _text.length()) {
        return _text.substring(_idx);
return "";
Stringsubstring(String baseString, int start, int end)
substring
if (baseString == null)
    return null;
else if (start >= baseString.length() || start < 0 || end < 0 || start > end)
    return null;
else if (end >= baseString.length())
    return baseString.substring(start);
else {
    return baseString.substring(start, end);
...
StringsubString(String input, char start, char end)
sub String
int intStart = input.indexOf(String.valueOf(start));
if (intStart < 0) {
    return input;
intStart++;
String ret = input.substring(intStart);
int intEnd = ret.indexOf(String.valueOf(end));
if (intEnd < 0) {
...
Stringsubstring(String input, int expectedLength)
substring
String output = input;
if (input != null && input.length() > expectedLength) {
    output = input.substring(0, expectedLength);
return output;
Stringsubstring(String input, int maxLength)
substring
if (input == null)
    return null;
else
    return (input.length()) <= maxLength ? input : input.substring(0, maxLength);
Stringsubstring(String input, int maxLength)
substring
if (input == null) {
    return null;
if (maxLength < 0) {
    return input;
if (input.length() > maxLength) {
    return input.substring(0, maxLength);
...
StringsubString(String input, String startStr, String endStr)
sub String
try {
    int idxStart = input.indexOf(startStr);
    if (idxStart == -1)
        return "";
    int idxEnd = input.indexOf(endStr, idxStart + 1 + startStr.length());
    if (idxStart == -1 || idxEnd == -1)
        return "";
    return input.substring(idxStart + startStr.length(), idxEnd).trim();
...
StringsubString(String name)
sub String
if (name == null)
    return null;
String[] nameStr = name.split("_");
StringBuffer buffer = new StringBuffer();
for (int i = 2; i < nameStr.length; i++) {
    buffer.append(nameStr[i]);
    buffer.append("_");
String returnStr = null;
if (buffer.toString().endsWith("_")) {
    returnStr = buffer.toString().substring(0, buffer.toString().lastIndexOf("_"));
return returnStr;
Stringsubstring(String origin, int len)
substring
if (origin == null || origin.equals("") || len < 1)
    return "";
byte[] strByte = new byte[len];
if (len > origin.getBytes().length) {
    return origin;
System.arraycopy(origin.getBytes(), 0, strByte, 0, len);
int count = 0;
...
StringsubString(String original, String regex)
sub String
int end;
boolean found = false;
for (end = 1; end < original.length(); end++) {
    if (original.substring(0, end).matches(regex)) {
        found = true;
    } else if (found)
        return original.substring(0, end - 1);
if (original.substring(0, original.length() - 1).matches(regex))
    return original.substring(0, original.length() - 1);
if (original.substring(0, original.length()).matches(regex))
    return original;
return "";