StringUtils.java 9.9 KB
   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
 444
 445
 446
 447
/**
* Copyright (c) 2011-2014, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.toolkit;

import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.enums.SQLlikeType;

/**
* <p>
* String 工具类
* </p>
*
* @author D.Yang
* @Date 2016-08-18
*/
public class StringUtils {

/**
* 空字符
*/
public static final String EMPTY = "";

/**
* 下划线字符
*/
public static final char UNDERLINE = '_';

/**
* 占位符
*/
public static final String PLACE_HOLDER = "{%s}";

/**
* <p>
* 判断字符串是否为空
* </p>
*
* @param cs
* 需要判断字符串
* @return 判断结果
*/
public static boolean isEmpty(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}

/**
* <p>
* 判断字符串是否不为空
* </p>
*
* @param cs
* 需要判断字符串
* @return 判断结果
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}

/**
* <p>
* 字符串驼峰转下划线格式
* </p>
*
* @param param
* 需要转换的字符串
* @return 转换好的字符串
*/
public static String camelToUnderline(String param) {
if (isEmpty(param)) {
return EMPTY;
}
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) && i > 0) {
sb.append(UNDERLINE);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}

/**
* <p>
* 字符串下划线转驼峰格式
* </p>
*
* @param param
* 需要转换的字符串
* @return 转换好的字符串
*/
public static String underlineToCamel(String param) {
if (isEmpty(param)) {
return EMPTY;
}
String temp = param.toLowerCase();
int len = temp.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = temp.charAt(i);
if (c == UNDERLINE) {
if (++i < len) {
sb.append(Character.toUpperCase(temp.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
}

/**
* <p>
* 判断字符串是否为纯大写字母
* </p>
*
* @param str
* 要匹配的字符串
* @return
*/
public static boolean isUpperCase(String str) {
return match("^[A-Z]+$", str);
}

/**
* <p>
* 正则表达式匹配
* </p>
*
* @param regex
* 正则表达式字符串
* @param str
* 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
public static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}

/**
* <p>
* SQL 参数填充
* </p>
*
* @param content
* 填充内容
* @param args
* 填充参数
* @return
*/
public static String sqlArgsFill(String content, Object... args) {
if (null == content) {
return null;
}
if (args != null) {
int length = args.length;
if (length >= 1) {
for (int i = 0; i < length; i++) {
// 改造 String.replace() 用法
content = Pattern.compile(String.format(PLACE_HOLDER, i), Pattern.LITERAL).matcher(content)
.replaceAll(sqlParam(args[i]));
}
}
}
return content;
}

/**
* 获取SQL PARAMS字符串
*
* @param obj
* @return
*/
public static String sqlParam(Object obj) {
String repStr;
if (obj instanceof Collection) {
repStr = StringUtils.quotaMarkList((Collection<?>) obj);
} else {
repStr = StringUtils.quotaMark(obj);
}
return repStr;
}

/**
* <p>
* 使用单引号包含字符串
* </p>
*
* @param obj
* 原字符串
* @return 单引号包含的原字符串
*/
public static String quotaMark(Object obj) {
String srcStr = String.valueOf(obj);
if (obj instanceof String) {
// fix #79
return StringEscape.escapeString(srcStr);
}
return srcStr;
}

/**
* <p>
* 用%连接like
* </p>
*
* @param str
* 原字符串
* @return
*/
public static String concatLike(String str, SQLlikeType type) {
switch (type) {
case LEFT:
str = "%" + str;
break;
case RIGHT:
str += "%";
break;
default:
str = "%" + str + "%";
}
return StringEscape.escapeString(str);
}

/**
* <p>
* 使用单引号包含字符串
* </p>
*
* @param coll
* 集合
* @return 单引号包含的原字符串的集合形式
*/
public static String quotaMarkList(Collection<?> coll) {
StringBuilder sqlBuild = new StringBuilder();
sqlBuild.append("(");
int _size = coll.size();
int i = 0;
Iterator<?> iterator = coll.iterator();
while (iterator.hasNext()) {
String tempVal = StringUtils.quotaMark(iterator.next());
if (i + 1 == _size) {
sqlBuild.append(tempVal);
} else {
sqlBuild.append(tempVal);
sqlBuild.append(",");
}
i++;
}
sqlBuild.append(")");
return sqlBuild.toString();
}

/**
* <p>
* 拼接字符串第二个字符串第一个字母大写
* </p>
*
* @param concatStr
* @param str
* @return
*/
public static String concatCapitalize(String concatStr, final String str) {
if (isEmpty(concatStr)) {
concatStr = EMPTY;
}
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}

final char firstChar = str.charAt(0);
if (Character.isTitleCase(firstChar)) {
// already capitalized
return str;
}

StringBuilder sb = new StringBuilder(strLen);
sb.append(concatStr);
sb.append(Character.toTitleCase(firstChar));
sb.append(str.substring(1));
return sb.toString();
}

/**
* <p>
* 字符串第一个字母大写
* </p>
*
* @param str
* @return
*/
public static String capitalize(final String str) {
return concatCapitalize(null, str);
}

/**
* <p>
* 判断对象是否为空
* </p>
*
* @param object
* @return
*/
public static boolean checkValNotNull(Object object) {
if (object instanceof CharSequence) {
return isNotEmpty((CharSequence) object);
}
return object == null ? false : true;
}

/**
* <p>
* 判断对象是否为空
* </p>
*
* @param object
* @return
*/
public static boolean checkValNull(Object object) {
return !checkValNotNull(object);
}

// endsWith
// -----------------------------------------------------------------------

/**
* <p>
* Check if a String ends with a specified suffix.
* </p>
*
* <p>
* <code>null</code>s are handled without exceptions. Two <code>null</code>
* references are considered to be equal. The comparison is case sensitive.
* </p>
*
* <pre>
* StringUtils.endsWith(null, null) = true
* StringUtils.endsWith(null, "abcdef") = false
* StringUtils.endsWith("def", null) = false
* StringUtils.endsWith("def", "abcdef") = true
* StringUtils.endsWith("def", "ABCDEF") = false
* </pre>
*
* @see java.lang.String#endsWith(String)
* @param str
* the String to check, may be null
* @param suffix
* the suffix to find, may be null
* @return <code>true</code> if the String ends with the suffix, case
* sensitive, or both <code>null</code>
* @since 2.4
*/
public static boolean endsWith(String str, String suffix) {
return endsWith(str, suffix, false);
}

/**
* <p>
* Case insensitive check if a String ends with a specified suffix.
* </p>
*
* <p>
* <code>null</code>s are handled without exceptions. Two <code>null</code>
* references are considered to be equal. The comparison is case
* insensitive.
* </p>
*
* <pre>
* StringUtils.endsWithIgnoreCase(null, null) = true
* StringUtils.endsWithIgnoreCase(null, "abcdef") = false
* StringUtils.endsWithIgnoreCase("def", null) = false
* StringUtils.endsWithIgnoreCase("def", "abcdef") = true
* StringUtils.endsWithIgnoreCase("def", "ABCDEF") = false
* </pre>
*
* @see java.lang.String#endsWith(String)
* @param str
* the String to check, may be null
* @param suffix
* the suffix to find, may be null
* @return <code>true</code> if the String ends with the suffix, case
* insensitive, or both <code>null</code>
* @since 2.4
*/
public static boolean endsWithIgnoreCase(String str, String suffix) {
return endsWith(str, suffix, true);
}

/**
* <p>
* Check if a String ends with a specified suffix (optionally case
* insensitive).
* </p>
*
* @see java.lang.String#endsWith(String)
* @param str
* the String to check, may be null
* @param suffix
* the suffix to find, may be null
* @param ignoreCase
* inidicates whether the compare should ignore case (case
* insensitive) or not.
* @return <code>true</code> if the String starts with the prefix or both
* <code>null</code>
*/
private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
if (str == null || suffix == null) {
return (str == null && suffix == null);
}
if (suffix.length() > str.length()) {
return false;
}
int strOffset = str.length() - suffix.length();
return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
}

}