Android String Trim trim(String trimStr, String trimChars)

Here you can find the source of trim(String trimStr, String trimChars)

Description

trim

License

Open Source License

Declaration

public final static String trim(String trimStr, String trimChars) 

Method Source Code

//package com.java2s;

public class Main {
    public final static String trim(String trimStr, String trimChars) {
        int spIdx = findFirstNotOf(trimStr, trimChars);
        if (spIdx < 0) {
            String buf = trimStr;
            return buf;
        }/*from ww  w.j a  v  a2 s . com*/
        String trimStr2 = trimStr.substring(spIdx, trimStr.length());
        spIdx = findLastNotOf(trimStr2, trimChars);
        if (spIdx < 0) {
            String buf = trimStr2;
            return buf;
        }
        String buf = trimStr2.substring(0, spIdx + 1);
        return buf;
    }

    public final static int findFirstNotOf(String str, String chars) {
        return findOf(str, chars, 0, (str.length() - 1), 1, false);
    }

    public final static int findLastNotOf(String str, String chars) {
        return findOf(str, chars, (str.length() - 1), 0, -1, false);
    }

    public final static int findOf(String str, String chars, int startIdx,
            int endIdx, int offset, boolean isEqual) {
        if (offset == 0)
            return -1;
        int charCnt = chars.length();
        int idx = startIdx;
        while (true) {
            if (0 < offset) {
                if (endIdx < idx)
                    break;
            } else {
                if (idx < endIdx)
                    break;
            }
            char strc = str.charAt(idx);
            int noEqualCnt = 0;
            for (int n = 0; n < charCnt; n++) {
                char charc = chars.charAt(n);
                if (isEqual == true) {
                    if (strc == charc)
                        return idx;
                } else {
                    if (strc != charc)
                        noEqualCnt++;
                    if (noEqualCnt == charCnt)
                        return idx;
                }
            }
            idx += offset;
        }
        return -1;
    }
}

Related

  1. trimSuffix(final String text, final String suffix)
  2. trimPrefix(final String text, final String prefix)
  3. trimSpace(String oldString)
  4. rightTrimSize(String s)
  5. trim(String s, char c)
  6. trimEnd(String s, String extraChars)
  7. trimStart(String s, String extraChars)
  8. betterTrim(String input)