首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

j2me工具种:TextUtil.java

2012-09-04 
j2me工具类:TextUtil.javaimport java.util.Vectorimport javax.microedition.lcdui.Fontimport javax.m

j2me工具类:TextUtil.java

import java.util.Vector;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;public final class TextUtil {    /**     * 分割字符串     * @param value 需要处理的字符串     * @param regex 分隔符     * @return 返回结果数组     */    public static String[] split(String value, String regex) {        if (value == null || value.equals("")) {            return null;        }        Vector split = new Vector();        while (true) { //循环切割            int index = value.indexOf(regex);            if (index == -1) {                if (value != null && !value.equals("")) {                    split.addElement(value);                    break;                }            }            split.addElement(value.substring(0, index));            value = value.substring(index + 1, value.length());        }        String[] s = new String[split.size()];        split.copyInto(s);        return s;    }    /**     * 按照指定宽度分隔字符串     * */    public static String[] split(String value, Font font, int width) {        String[] result = new String[1];        if (font.stringWidth(value) < width) {            result[0] = value;            return result;        } else {            char[] ch = value.toCharArray();            Vector lines = new Vector();            StringBuffer sb = new StringBuffer();            int w = 0;            for (int i = 0; i < ch.length; i++) {                w = w + font.charWidth(ch[i]);                sb.append(ch[i]);                if (w >= width - 12) {                    lines.addElement(sb.toString());                    sb.setLength(0);                    w = 0;                }            }            lines.addElement(sb.toString());            result = new String[lines.size()];            lines.copyInto(result);            lines.removeAllElements();            lines = null;            return result;        }    }        /**     * 拆分字符串     * sourceStr 要拆分字符串     * maxWidth 最大宽度     * split 分隔符集合     */    public static final Vector getSubsection(String sourceStr, int maxWidth, String split, Font font) {        Vector vector = new Vector();        String tmp = sourceStr;        int i, j;        int lastLength = 1;        int step = 0;        try {            while (!tmp.equals("")) {                i = tmp.indexOf("\n");                if (i > 0) {                    if (font.stringWidth(tmp.substring(0, i - 1)) > maxWidth) {                        i = -1;                    }                }                if (i == -1) {                    if (lastLength > tmp.length()) {                        i = tmp.length();                    } else {                        i = lastLength;                        step = font.stringWidth(tmp.substring(0, i)) > maxWidth ? -1 : 1;                        if (i < tmp.length()) {                            while (!(font.stringWidth(tmp.substring(0, i)) <= maxWidth && font.stringWidth(tmp.substring(0, i + 1)) > maxWidth)) {                                i = i + step;                                if (i == tmp.length()) {                                    break;                                }                            }                        }                    }                    if (!split.equals("")) {                        j = i;                        if (i < tmp.length()) {                            while (split.indexOf(tmp.substring(i - 1, i)) == -1) {                                i--;                                if (i == 0) {                                    i = j;                                    break;                                }                            }                        }                    }                }                lastLength = i;                vector.addElement(tmp.substring(0, i));                if (i == tmp.length()) {                    tmp = "";                } else {                    tmp = tmp.substring(i);                    if (tmp.substring(0, 1).equals("\n")) {                        tmp = tmp.substring(1);                    }                }            }        } catch (Exception e) {            System.out.println("getSub:" + e);        }        return vector;    }    /**     * 文字自动换行     * string 文字内容     * x 起始坐标     * y 起始坐标     * width 文本最大宽度     * color 文字颜色     * font 字体     */    public static final void drawString(Graphics g, String string, int x, int y, int width, int color, Font font) {        g.setColor(color);        Vector vector = getSubsection(string, width, "", font);        for (int i = 0; i < vector.size(); i++) {            g.drawString((String) vector.elementAt(i), x, y + (font.getHeight() + 2) * i + 1, 20);        }        vector = null;    }    /**     * 分割字符串显示,让菜单显示更漂亮,让界面显示的更完美^_^     * 指定width以外的内容将用"..."显示     */    public static String devide(String value, Font font, int width) {        if (font.stringWidth(value) < width) {            return value;        }        char[] ch = value.toCharArray();        StringBuffer sb = new StringBuffer();        int w = 0;        for (int i = 0; i < ch.length; i++) {            w = w + font.charWidth(ch[i]);            sb.append(ch[i]);            if (w >= width) {                sb.append("..."); //增加几个点会美观                return sb.toString();            }        }        return sb.toString();    }    /**     * 提供常用的字符累加     * 通过操作符号"+"是很耗内存的方法,所以提供一个StringBuffer的方法     */    public final static String append(String one, String two) {        StringBuffer sb = new StringBuffer();        sb.append(one);        sb.append(two);        return sb.toString();    }    public static boolean wrongCardNumber(String inputCardNumber) {        int key = -1;        int len = inputCardNumber.length();        if (len < 16) {            return true;        }        String tmp = inputCardNumber.substring(0, len - 1);        int[] num = new int[tmp.length()];        for (int i = 0; i < len; i++) {            if (inputCardNumber.charAt(i) < '0' && inputCardNumber.charAt(i) > '9') {                return true;            }        }        for (int i = 0; i < num.length; i++) {            num[i] = Integer.parseInt(tmp.substring(i, i + 1));        }        int total = 0;        for (int i = num.length - 1; i >= 0; i -= 2) {            total += (2 * num[i] / 10 + 2 * num[i] % 10);        }        for (int i = num.length - 2; i >= 0; i -= 2) {            total += num[i];        }        if (total % 10 == 0) {            key = 0;        } else {            key = (total / 10 + 1) * 10 - total;        }        if (key == Integer.parseInt(inputCardNumber.substring(len - 1, len))) {            return false;        }        return true;    }//卡号4位4位显示,型如:1111-1111-1111-1111    public static String formatNumber(String input) {       int length = input.length();StringBuffer sb = new StringBuffer();// int j=0;for (int i = 0; i < length; i++) {if (i % 4 == 0 && i != 0) {sb.append('-');sb.append(input.charAt(i));} else {sb.append(input.charAt(i));}}return sb.toString();   }}

热点排行