Java ASCII to String asciiTrimR(String str, int length)

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

Description

ascii Trim R

License

Apache License

Declaration

public static String asciiTrimR(String str, int length) 

Method Source Code

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

public class Main {

    public static String asciiTrimR(String str, int length) {
        int alen = asciiLength(str);
        if (alen <= length)
            return str;
        String result = "";
        alen = 0;//  w  w  w . ja  v a  2 s .  c o m
        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;
    }
}

Related

  1. AsciiToChar(int asc)
  2. asciiToLowerCase(String s)
  3. asciiToString(byte ascii)
  4. asciiToString(byte[] b, int off, int len)
  5. asciiToString(int asciiCode)