Java String Sub String subStrByBytes(String str, int len, String tail)

Here you can find the source of subStrByBytes(String str, int len, String tail)

Description

sub Str By Bytes

License

Apache License

Declaration

public static String subStrByBytes(String str, int len, String tail) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String subStrByBytes(String str, int len, String tail) {
        if (str == null) {
            return "";
        }//w  w w.  j a  v a  2s  .c om
        if (str.getBytes().length <= len) {
            return str.trim();
        }

        str = str.trim();
        String s = "";
        char[] c = str.toCharArray();
        int i = 0;
        if (tail != null) {
            len -= tail.getBytes().length;
        }
        while (s.getBytes().length < len) {
            s = s + String.valueOf(c[i]);
            i++;
        }
        if (s.getBytes().length > len) {
            s = s.substring(0, s.length() - 1);
        }
        if (tail != null)
            s = s + tail;
        return s;
    }
}

Related

  1. substract(int[] array1, int[] array2)
  2. substract(Number n1, Number n2)
  3. substraction2(double[] a, double[] b)
  4. substractPrefixPostfix(Object obj, String prefix, String suffix)
  5. subStrBeforeDotNotIncludeDot(String str)
  6. substrByLength(String str, int start, int size, String markCode)
  7. subStrBytes2(String str, int byteLength)
  8. substrCount(String haystack, String needle)
  9. subStrIfNeed(final String str, int num)