StringUtils

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/需要导入第三方jar包pinyin4j.jar
 
import net.sourceforge.pinyin4j.PinyinHelper;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class StringUtils {
   protected static final String TAG = StringUtils.class.getSimpleName();
 
   /**
    * 增加空白
    */
   public static String addBlank(int size) {
      return String.format("%" + size + "s", "");
   }
 
   /**
    * 判断字符串是否为null或者""
    */
   public static boolean isEmptyOrNull(String content) {
      if (content == null || content.equals("")) {
         return true;
      }
      return false;
   }
 
   /**
    *  判断字符串是否为IP地址
    */
   public static boolean isIPAddress(String ipString) {
      if (ipString != null) {
         String[] singleArray = ipString.split("\\.");
         if (singleArray == null) {
            return false;
         }
         for (String numString : singleArray) {
            if (isEmptyOrNull(numString.trim())) {
               return false;
            }
            try {
               int num = Integer.parseInt(numString.trim());
               if (num < 0 || num > 255) {
                  return false;
               }
            } catch (NumberFormatException e) {
               return false;
            }
 
         }
         return true;
      }
      return false;
   }
 
   /**
    * 是否是email地址
    */
   public static boolean isEmailAddress(String emailString) {
      String format = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";
      return isMatch(format, emailString);
   }
 
   /**
    * 是否为数字
    */
   public static boolean isDigit(String digitString) {
      if (!isEmptyOrNull(digitString)) {
         String regex = "[0-9]*";
         return isMatch(regex, digitString);
      }
      return false;
   }
 
   /**
    *  字符串正则校验
    */
   public static boolean isMatch(String regex, String string) {
 
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(string);
      return matcher.matches();
   }
 
   /**
    *是否为URL地址
    */
   public static boolean isUrl(String strIp) {
      String strPattern = "^((https?)|(ftp))://(?:(\\s+?)(?::(\\s+?))?@)?([a-zA-Z0-9\\-.]+)"
            + "(?::(\\d+))?((?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?$";
      return isMatch(strPattern, strIp);
   }
 
   /**
    *  String 转换成Unicode
    * @param string
    *            传入汉字
    * @return
    */
   public static String string2Unicode(String string) {
      if (!isEmptyOrNull(string)) {
         char[] charArray = string.toCharArray();
         StringBuffer buffer = new StringBuffer();
         for (char ch : charArray) {
            int code = (int) ch;
            buffer.append(code);
         }
         return buffer.toString();
      }
      return null;
   }
 
   /**
    *  Unicode转换成String
    * @param string
    * @return
    */
   public static String unicode2String(String string) {
      if (!isEmptyOrNull(string)) {
         int end = 0;
         String noSpace = string.trim();
         int count = noSpace.length() / 5;
         StringBuffer buffer = new StringBuffer();
 
         for (int j = 0; j < count; j++) {
            end += 5;
            int uCode = Integer.valueOf(noSpace.substring(j * 5, end));
            buffer.append((char) uCode);
 
         }
         return buffer.toString();
      }
      return null;
   }
 
   /**
    *  得到首字母
    */
   public static String getPinYinFirstLetter(String string) {
      if (!isEmptyOrNull(string)) {
         char[] cs = string.toCharArray();
         for (int i = 0; i < cs.length; i++) {
            String[] pinyins = null;
            if (cs[i] > 128) {
               pinyins = PinyinHelper.toHanyuPinyinStringArray(cs[i]);
            }
            if (pinyins != null && pinyins.length > 0) {
               return pinyins[0].substring(0, 1);
            }
         }
      }
      return null;
   }
 
   /**
    * 获取url参数
    */
   public static String getParamValueOfUrl(String url, String paramName) {
      try {
         String urls[] = url.split("[?]");
         if (urls.length > 1) {
            String param = urls[1];
            String params[] = param.split("[&]");
            for (String string : params) {
               String keyAndValue[] = string.split("[=]");
               if (keyAndValue.length > 1) {
                  String key = keyAndValue[0];
                  String value = keyAndValue[1];
                  if (key.equalsIgnoreCase(paramName)) {
                     return value;
                  }
               }
            }
         }
      } catch (Exception e) {
         return "";
      }
      return "";
   }
 
   /**
    * 半角转换为全角
    * @param input
    * @return
   */
   public static String ToDBC(String input) {
      char[] c = input.toCharArray();
      for (int i = 0; i < c.length; i++) {
         if (c[i] == 12288) {
            c[i] = (char) 32;
            continue;
         }
         if (c[i] > 65280 && c[i] < 65375)
            c[i] = (char) (c[i] - 65248);
      }
      return new String(c);
   }
 
   /**
    * 去除特殊字符或将所有中文标号替换为英文标号
    * @param str
    * @return
   */
   public static String stringFilter(String str) {
      str = str.replaceAll("【", "[").replaceAll("】", "]").replaceAll("!", "!").replaceAll(":", ":");// 替换中文标号
      String regEx = "[『』]"; // 清除掉特殊字符
      Pattern p = Pattern.compile(regEx);
      Matcher m = p.matcher(str);
      return m.replaceAll("").trim();
   }
 
   /**
    * 根据Unicode编码完美的判断中文汉字和符号
    * @param c
    * @return
    */
 
   private static boolean isChinese(char c) {
 
      Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
 
      if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
 
      || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
 
      || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
 
      || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
 
      || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
 
      || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
 
      || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
 
         return true;
 
      }
 
      return false;
 
   }
 
   /**
    * 完整的判断中文汉字和符号
    * @param strName
    * @return
    */
 
   public static boolean isChinese(String strName) {
 
      char[] ch = strName.toCharArray();
 
      for (int i = 0; i < ch.length; i++) {
 
         char c = ch[i];
 
         if (isChinese(c)) {
 
            return true;
 
         }
 
      }
 
      return false;
 
   }
 
public static String parseStream2Str(InputStream is) {
      //内存输出流
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int len = -1;
      byte[] buffer = new byte[1024*8];
      try {
         while((len = is.read(buffer)) != -1){
            baos.write(buffer, 0, len);
         }
         return new String(baos.toByteArray());
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }
}

 

posted on   LoaderMan  阅读(179)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示