Android String Sub String Get substring(String str, int len, String more)

Here you can find the source of substring(String str, int len, String more)

Description

substring

License

Open Source License

Declaration

public static String substring(String str, int len, String more) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String substring(String str, int toCount) {
        return substring(str, toCount, "..");
    }/*from  ww w .  j a va 2 s. co m*/

    public static String substring(String str, int len, String more) {
        if (str == null || "".equals(str) || len < 1) {
            return "";
        }
        char[] chars = str.toCharArray();
        int count = 0;
        int charIndex = 0;
        for (int i = 0; i < chars.length; i++) {
            int charLength = getCharLen(chars[i]);
            if (count <= len - charLength) {
                count += charLength;
                charIndex++;
            } else {
                break;
            }
        }
        if (charIndex == chars.length) {
            return new String(chars, 0, charIndex);
        } else {
            return new String(chars, 0, charIndex) + more;
        }
    }

    private static int getCharLen(char c) {
        int k = 0x80;
        return c / k == 0 ? 1 : 2;
    }
}

Related

  1. subStringEndString(String sourceStr, String endString)
  2. hightLinghtString(String destStr, String subStr, int color)
  3. replace(String source, String subject, String object)
  4. replaceOnce(String source, String subject, String object)
  5. substring(String str, int toCount)
  6. substringFromLast(final String str, final String separator)
  7. substringToLast(final String str, final String separator)
  8. takeOutFirstChar(String input)
  9. substring(String str, int srcPos, int specialCharsLength)