复制代码
1
2
3
4
5
6
7
8
9
10int month = 6; int day = 13; Map<String,Object> map = new HashMap<String, Object>(); map.put("month",String.valueOf(month)); map.put("day",String.valueOf(day)); String template = "申请于${month}月${day}日已审核通过,款项将于7个工作日返回支付宝账户。请注意查收。"; String str = StringUtils.reString(template, map); System.out.println(str);
打印结果:申请于6月13日已审核通过,款项将于7个工作日返回支付宝账户。请注意查收。
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.text.SimpleDateFormat; import java.util.*; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * 功能描述:字符串的操作类 * * 2010-12-30 上午11:37:27 */ public class StringUtils extends org.apache.commons.lang.StringUtils { /** * 检查指定的字符串列表是否不为空。 * * @param values 字符串列表 * @return true/false */ public static boolean areNotEmpty(String... values) { boolean result = true; if (values == null || values.length == 0) { result = false; } else { for (String value : values) { result &= !isEmpty(value); } } return result; } /** * 获取非null的字符串 * @param str * @return */ public static String getNotNull( String str ){ return str == null ? "" : str; } /** * 获取非null的字符串 * @param str * @return */ public static String getNotNull( Object str ){ return str == null ? "" : str.toString(); } /** * 计算流水号 * @param curr * @param length * @return */ public static String getSerialNumber( int curr , int length ){ return getSerialNumber( EMPTY , curr , length ); } /** * 截取字符串,以字节截取。 * {@code return substring(s, length, true)} * * @param s * @param length * @param more .... * @return */ public static String substring(String s, int length,String more) { return substring(s, length, true, more); } /** * 截取字符串,以字节截取。 * {@code return substring(s, length, true)} * * @param s * @param length * @return */ public static String substring(String s, int length) { return substring(s, length, true, ""); } /** * 截取字符串,以字节截取,如果截取的最后一个字符为双字节,根据ignore决定是否省略 * * @param s * @param length * @param ignore * @return */ public static String substring(String s, int length, boolean ignore,String more) { if( s == null || isEmpty( s ) ) return EMPTY; int oldLength = s.length(); if( oldLength <= length / 2 ) return s; char[] c = s.toCharArray(); StringBuilder builder = new StringBuilder(length); int len = 0; for (int i = 0; i < length && i < c.length; i++) { if (len < length) { if (c[i] / 0x80 == 0) { // 单字节 builder.append(c[i]); len++; } else { // 双字节 if (len + 2 <= length || !ignore) { builder.append(c[i]); len += 2; } else { break; } } } } if( !isEmpty(more) || oldLength != builder.toString().length() )builder.append( more ); return builder.toString(); } /** * 正则表达式 * * @param s * @param regEx * @return */ public static Boolean regexString(String s, String regEx) { Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(s); boolean result = matcher.matches(); return result; } /** * 过滤掉所有空白<br> * * @param str * 字符串 * @return */ public static String trimAllSpace(String str) { if (isEmpty(str)) return EMPTY; return trim(str).replace("t", StringUtils.EMPTY).replaceAll("\s", StringUtils.EMPTY).replaceAll(" ", StringUtils.EMPTY); } /** * 通过正则表达式对字符串进行分割 * * @param str * 字符串 * @param regex * 正则表达式 * @return */ public static String[] splitRegex(String str, String regex) { String[] array = new String[0]; if (isEmpty(str) || isEmpty(regex)) return array; array = str.split(regex); if (array == null) return new String[0]; return array; } /** * 连接字符串,默认没有分隔符 * @param str * @return */ public static String join( String[] str ){ return join("", str); } /** * 连接字符串 * @param joinAt 分隔符 * @param str * @return */ public static String join( String joinAt,String[] str ){ String response = ""; boolean first = true; for(String s : str ){ if( first ){ response += s; first = false; }else{ response += ( joinAt + s ); } } return response; } /** * 计算流水号 * @param curr * @param length * @return */ public static String getSerialNumber(String prefix, int curr , int length ){ StringBuffer sb = new StringBuffer(); if( String.valueOf( curr ).length() == length ){ return prefix + String.valueOf( curr ); } sb.append( curr ); while( sb.length() < length ){ sb.insert(0, "0" ); } sb.insert( 0 , prefix ); return sb.toString(); } /** * 通过当前的code计算下一个code * @param code * @param lastLength 最后的code长度 * for example: StringUtils.nextCode( "0102",2 ); 返回0103 * @return */ public static String nextCode( String code , int lastLength ){ if( StringUtils.isEmpty( code ) ) return EMPTY; if( code.length() < lastLength ) return EMPTY; String prefix = code.substring( 0 , code.length() - lastLength ); String last = code.substring( code.length() - lastLength , code.length() ); return getSerialNumber(prefix, NumberUtil.toInt(last) + 1, lastLength); } /** * 按照长度分隔字符串 如: splitByLength( "aaaaaa" , 4) 返回结果 : String[]{"aaaa","aa"} * @param length * @return */ public static String[] splitByLength(String str , int length ){ if( StringUtils.isEmpty( str ) ) return null; int size = str.length(); List<String> response = new ArrayList<String>(); for(int i=0;i<size;i+=length){ response.add( StringUtils.substring( str , i , i+length ) ); } String[] strArray = new String[ response.size() ]; response.toArray(strArray); return strArray; } /** * 按照长度获取编号 如: getCodes( "aaaaaa" , 4) 返回结果 : String[]{"aaaa","aaaaaa"} * @param length * @return */ public static String[] getCodes(String str , int length ){ if( StringUtils.isEmpty( str ) ) return null; int size = str.length(); List<String> response = new ArrayList<String>(); for(int i=0;i<size;i+=length){ response.add( StringUtils.substring( str , 0 , i+length ) ); } String[] strArray = new String[ response.size() ]; response.toArray( strArray ); return strArray; } /** * 中文字符检验 * @param s String * @return 包含中文字符返回true,否则返回false * */ public static boolean chineseValid(String s){ if(ObjectUtil.isEmpty(s)){ return false; } int length = s.length(); byte [] b; for(int i = 0;i<length;i++) { b = s.substring(i).getBytes(); if ((b[0]&0xff)>128) return true; } return false; } public static Pager urlToPager( String url ) { Pager pager = new Pager(); if( StringUtils.isEmpty( url )) { return pager; } else { Map<String,Object> pagerQuesy = new HashMap<String,Object>(); String [] querys =url.split("&"); for( String query : querys ) { query = query.replace("query[", "").replace("]", ""); String [] queryStr = query.split("="); if(queryStr.length!=2) continue; pagerQuesy.put(queryStr[0], queryStr[1]); } try{ if( pagerQuesy.get("page") != null&&!"".equals(pagerQuesy.get("page"))) pager.setPage(Integer.valueOf((String) pagerQuesy.get("page"))); if( pagerQuesy.get("pageSize") != null&&!"".equals(pagerQuesy.get("pageSize"))) pager.setPageSize(Integer.valueOf((String) pagerQuesy.get("pageSize"))); if( pagerQuesy.get("totalNum") != null&&!"".equals(pagerQuesy.get("totalNum"))) pager.setTotalNum(Integer.valueOf((String) pagerQuesy.get("totalNum"))); if( pagerQuesy.get("totalPage") != null&&!"".equals(pagerQuesy.get("totalPage"))) pager.setTotalPage(Integer.valueOf((String) pagerQuesy.get("totalPage"))); }catch (Exception e) { } pager.setQuery(pagerQuesy); return pager; } } public static String mapToURL ( Map<String,Object > map ) { if( map == null || map.isEmpty() ) return ""; StringBuffer sb = new StringBuffer("?"); Iterator<Entry<String, Object >> map_iterator= map.entrySet().iterator(); while ( map_iterator .hasNext() ) { Entry<String, Object> mapObjct = map_iterator.next(); String [] value = (String [])mapObjct.getValue() ; if( value != null && value.length > 0 ) sb.append( mapObjct.getKey() ).append("=").append( value[0] ).append("&"); } return sb.toString(); } public static String intListToUrl(Integer [] list ) { StringBuffer sb = new StringBuffer(); for( Integer id : list ) { if(id !=null) sb.append(id).append(","); } return sb.toString(); } /** * * 写一个简单的 解析 方法 * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * * * */ public static String reString ( String model , Map< String, Object > map ) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException { model = model.replace("}", "} "); List< String > objModel = getModelViewFile(model); for( String model_obj : objModel ) { String patch = model_obj.replace("{", "").replace("}", ""); String obj_string = getObjString( map.get( patch.split("\.")[0] ), StringUtils.join( "." , removeListThis( patch.split("\.") ) ) ); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm") ; try{ model = model.replace( "#$" + model_obj +" ", obj_string == null ? "" : sdf.format(new Date(Long.valueOf(obj_string) * 1000)) ); } catch(Exception ex){ } model = model.replace( "$" + model_obj +" ", obj_string == null ? "" : obj_string ); } return model; } public static List<String> getModelViewFile(String s) { List<String> PString = new ArrayList<String>(); Pattern pattern = Pattern.compile("\$(\S)*"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { String ss = matcher.group(); String[] res = ss.split("\$"); String a = res[1]; String b = a.split(""")[0]; b = b.split(" ")[0]; b = b.split(">")[0]; b = b.split(",")[0]; PString.add(b); } return PString; } private static String getObjString( Object obj ,String objStr ) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException { if( obj == null ){ return null; } //如果是字符串类型,则直接返回 if( obj instanceof String ) { return (String) obj; } if( obj instanceof Map<?,?> ) { Map< ? , ? > map = (Map<?,?>)obj; return "" + map.get( objStr ); } String objs [] = objStr.split("\."); Object object__ = getValue( obj , objs[0] ); String [] objs__ = removeListThis(objs); String objStr__ = join(".", objs__); if( objs.length == 1 ) { return object__.toString(); } return getObjString( object__ , objStr__ );//递归 } private static Object getValue( Object obj , String filedName ) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { PropertyDescriptor descriptor = new PropertyDescriptor(filedName, obj.getClass()); return descriptor.getReadMethod().invoke(obj); } /** * * 丢掉 当前 也就是 第一个元素 * * */ private static String [] removeListThis ( String [] objs ) { if( objs .length == 1 ) { return objs ; } else { String [] objs__ = new String [ objs.length -1 ]; for( int i = 0 ; i< objs.length-1 ; i++ ) { objs__ [i] = objs [ i+1 ]; } return objs__; } } public static String toUpCaseFirstChar(String data) { return data.substring(0,1).toUpperCase() + data.substring(1); } public static String jscb(String cbName ,String data ) { return StringUtils.areNotEmpty( cbName ) ? cbName+"("+data+");": data; } }
最后
以上就是微笑白昼最近收集整理的关于赋值变量给短信模板的全部内容,更多相关赋值变量给短信模板内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复