Java ASCII asciiPaddingR(String str, int length, String padding)

Here you can find the source of asciiPaddingR(String str, int length, String padding)

Description

ascii Padding R

License

Apache License

Declaration

public static String asciiPaddingR(String str, int length,
        String padding) 

Method Source Code

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

public class Main {

    public static String asciiPaddingR(String str, int length,
            String padding) {/*  ww w. j  av  a  2s .  co m*/
        String rst = asciiTrimR(str, length);
        int alen = asciiLength(rst);
        if (alen == length)
            return rst;
        rst += repeat(padding, (length - alen) / asciiLength(padding));
        return rst;
    }

    public static String asciiTrimR(String str, int length) {
        int alen = asciiLength(str);
        if (alen <= length)
            return str;
        String result = "";
        alen = 0;
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            alen += c > 127 ? 2 : 1;
            if (alen <= length)
                result += c;
            else
                break;
        }
        return result;
    }

    public static int asciiLength(String str) {
        int length = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            length += c > 127 ? 2 : 1;
        }

        return length;
    }

    public static String repeat(String str, int times) {
        String r = "";
        for (int i = 0; i < times; i++)
            r += str;
        return r;
    }
}

Related

  1. asciiEqualsIgnoreCase(byte[] buf1, byte[] buf2)
  2. asciiFill2(String str, int startIdx, String fillStr)
  3. asciify(String s)
  4. asciiIdx2StrIdx(String str, int asciiIdx)
  5. asciiLength(String str)
  6. asciiQuads(String word)
  7. asciiString(byte[] bytes, int from, int count)
  8. asciiString(String fourcc)
  9. AsciiStringToString(String content)