fengxiang
2018-05-04 4a41bd4e105385b5460e5a81c8b67e5f701a262b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.moral.common.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class StringUtils {
    public static final char UNDERLINE = '_';
 
    /**
     * 驼峰格式字符串转换为下划线格式字符串
     * 
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
 
    
    /**
     * //首字母转大写
     * 
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s){
        StringBuilder sb = new StringBuilder(s);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }
    public static boolean isNullOrEmpty(String toTest) {
        return toTest == null || toTest.length() == 0;
    }
    public static Object stringToObject4Type(Class<?> type,String value) throws Exception{
        Object result = value;
        if (type == double.class || type == Double.class) {
            result = Double.parseDouble(value);
        } else if (type == float.class || type == Float.class) {
            result = Float.parseFloat(value);
        } else if (type == long.class || type == Long.class) {
            result = Long.parseLong(value);
        } else if (type == int.class || type == Integer.class) {
            result = Integer.parseInt(value);
        } else if (type == short.class || type == Short.class) {
            result = Short.parseShort(value);
        } else if (type == boolean.class || type == Boolean.class) {
            result = Boolean.parseBoolean(value);
        } else if (type == Date.class) {
            value = value.replace("T", " ");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                result = df.parse(value);
            } catch (Exception e) {
                df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                try {
                    result = df.parse(value);
                } catch (ParseException e1) {
                    df = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        result = df.parse(value);
                    } catch (ParseException e2) {
                    }
                }
            }
        }
        return result;
    }
 
    /**
     *  中英文字符长度控制
     * @param str
     * @param maxLength
     * @return
     */
    public static String subStringCN(final String str, final int maxLength) {
        if (str == null) {
            return str;
        }
        String suffix = "...";
        int suffixLen = suffix.length();
 
        final StringBuffer sbuffer = new StringBuffer();
        final char[] chr = str.trim().toCharArray();
        int len = 0;
        for (int i = 0; i < chr.length; i++) {
 
            if (chr[i] >= 0xa1) {
                len += 2;
            } else {
                len++;
            }
        }
 
        if(len<=maxLength){
            return str;
        }
 
        len = 0;
        for (int i = 0; i < chr.length; i++) {
 
            if (chr[i] >= 0xa1) {
                len += 2;
                if (len + suffixLen > maxLength) {
                    break;
                }else {
                    sbuffer.append(chr[i]);
                }
            } else {
                len++;
                if (len + suffixLen > maxLength) {
                    break;
                }else {
                    sbuffer.append(chr[i]);
                }
            }
        }
        sbuffer.append(suffix);
        return sbuffer.toString();
    }
    public static boolean isNumericZidai(String str) {
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}