j 2 meツール類:TextUtil.java

8636 ワード


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("
"); 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("
")) { 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(); } }