StringUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package com.behosoft.util;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. public class StringUtils extends org.apache.commons.lang.StringUtils {
  7. private StringUtils() {
  8. }
  9. /**
  10. * <pre>
  11. * 判断指定的字符串str是否以withStr开头,比较方式忽略大小写
  12. * 例如:
  13. * startsIgroneCaseWith("abcde", "Abc") 返回true
  14. * startsIgroneCaseWith("abcde", "abc") 返回true
  15. * startsIgroneCaseWith("abcde", " abc") 返回false
  16. * startsIgroneCaseWith("abcde", null) 返回false
  17. * startsIgroneCaseWith("abcde", "") 返回true
  18. * </pre>
  19. *
  20. * @param str
  21. * @param withStr
  22. * @return
  23. */
  24. public static boolean startsIgroneCaseWith(String str, String withStr) {
  25. if (str == null || withStr == null)
  26. return false;
  27. else
  28. return str.regionMatches(true, 0, withStr, 0, withStr.length());
  29. }
  30. /**
  31. * <pre>
  32. * 判断指定的字符串str是否以withStr结尾,比较方式忽略大小写
  33. * 例如:
  34. * endsIgroneCaseWith("abcde", "Cde") 返回true
  35. * endsIgroneCaseWith("abcde", "cde") 返回true
  36. * endsIgroneCaseWith("abcde", "cde ") 返回false
  37. * endsIgroneCaseWith("abcde", null) 返回false
  38. * endsIgroneCaseWith("abcde", "") 返回true
  39. * </pre>
  40. *
  41. * @param str
  42. * @param withStr
  43. * @return
  44. */
  45. public static boolean endsIgroneCaseWith(String str, String withStr) {
  46. if (str == null || withStr == null)
  47. return false;
  48. else
  49. return str.regionMatches(true, str.length() - withStr.length(),
  50. withStr, 0, withStr.length());
  51. }
  52. /**
  53. * 指定的字符串是否以空白符开头
  54. *
  55. * @param str
  56. * @return
  57. */
  58. public static boolean startsWithWhiteSpace(String str) {
  59. if (str == null || str.length() < 1)
  60. return false;
  61. else
  62. return Character.isWhitespace(str.charAt(0));
  63. }
  64. /**
  65. * 指定的字符串是否以空白符结尾
  66. *
  67. * @param str
  68. * @return
  69. */
  70. public static boolean endsWithWhiteSpace(String str) {
  71. if (str == null || str.length() < 1)
  72. return false;
  73. else
  74. return Character.isWhitespace(str.charAt(str.length() - 1));
  75. }
  76. /**
  77. * 指定的字符串是否以空白符开头
  78. *
  79. * @param str
  80. * @return
  81. */
  82. public static boolean startsWithWhiteSpace(CharSequence str) {
  83. if (str == null || str.length() < 1)
  84. return false;
  85. else
  86. return Character.isWhitespace(str.charAt(0));
  87. }
  88. /**
  89. * 指定的字符串是否以空白符结尾
  90. *
  91. * @param str
  92. * @return
  93. */
  94. public static boolean endsWithWhiteSpace(CharSequence str) {
  95. if (str == null || str.length() < 1)
  96. return false;
  97. else
  98. return Character.isWhitespace(str.charAt(str.length() - 1));
  99. }
  100. /*
  101. * 功能描述: 删除指定的前导、后导子串(大小写敏感). <br>例子:
  102. * <br>StringUtils.trim("and and username = '123' and password = 'abc' and "
  103. * ,"and "); <br>结果:username = '123' and password = 'abc'
  104. *
  105. * @param sourceStr String 待删除的字符串
  106. *
  107. * @param ch String 子串
  108. *
  109. * @return String 处理后的字符串
  110. */
  111. public static String trim(String sourceStr, String ch) {
  112. if (null == sourceStr || "".equals(sourceStr.trim())) {
  113. return sourceStr;
  114. }
  115. if (null == ch || "".equals(ch)) {
  116. return sourceStr;
  117. }
  118. if (ch.length() > sourceStr.length()) {
  119. return sourceStr;
  120. }
  121. if (sourceStr.indexOf(ch) < 0) {
  122. return sourceStr;
  123. }
  124. // 删除前导
  125. int chLen = ch.length();
  126. /*
  127. * StringBuffer strBuf = new StringBuffer(sourceStr);
  128. * while(strBuf.indexOf(ch,0) == 0){ //表示还有前导
  129. * strBuf.replace(0,chLen,""); }
  130. *
  131. * //删除后导 int strBufLen = strBuf.length();
  132. * while(strBuf.indexOf(ch,(strBufLen - chLen)) == strBufLen - chLen){
  133. * strBuf.replace(strBuf.length() - chLen,strBuf.length(),""); strBufLen
  134. * = strBuf.length(); }
  135. */
  136. while (sourceStr.indexOf(ch, 0) == 0) { // 表示还有前导
  137. sourceStr = sourceStr.substring(chLen);
  138. }
  139. int strLen = sourceStr.length();
  140. while (sourceStr.indexOf(ch, (strLen - chLen)) == (strLen - chLen)) { // 表示还有后导
  141. sourceStr = sourceStr.substring(0, strLen - chLen);
  142. strLen = sourceStr.length();
  143. }
  144. return sourceStr;
  145. }
  146. /**
  147. * 功能描述: 用指定的分隔符把字符串数组合并成一个字符串. 数组为null或长度为0时返回空字符串 <BR>
  148. * 例子: <BR>
  149. * String[] array = {"1",null,"","3"}; <BR>
  150. * StringUtils.arrayToString(array,"|"); <BR>
  151. * 返回结果:1|||3
  152. *
  153. * @param array
  154. * String[] 待合并的数组
  155. * @param splitStr
  156. * String 数组各元素合并后,它们之间的分隔符,为null时用空字符串代替
  157. * @return String 合并后的字符串
  158. */
  159. public static String arrayToString(String[] array, String splitStr) {
  160. if (null == array || 0 == array.length) {
  161. return "";
  162. }
  163. if (null == splitStr) {
  164. splitStr = "";
  165. }
  166. StringBuilder strBuf = new StringBuilder("");
  167. int Len = array.length;
  168. for (int i = 0; i < Len - 1; i++) {
  169. strBuf.append((null == array[i]) ? "" : array[i]).append(splitStr);
  170. }
  171. strBuf.append((null == array[Len - 1]) ? "" : array[Len - 1]);
  172. return strBuf.toString();
  173. }
  174. /**
  175. * 功能描述: 用默认分隔符 | 把字符串数组合并成一个字符串. 数组为null或长度为0时返回空字符串
  176. *
  177. * @param array
  178. * String[] 待合并的数组
  179. * @return String 合并后的字符串
  180. */
  181. public static String arrayToString(String[] array) {
  182. return arrayToString(array, "|");
  183. }
  184. /**
  185. * 将null字符串转换为空串
  186. *
  187. * @param sourceStr
  188. * 待处理的字符串
  189. * @return String 处理的的字符串
  190. */
  191. public static String toVisualString(String sourceStr) {
  192. if (sourceStr == null || sourceStr.equals("")) {
  193. return "";
  194. } else {
  195. return sourceStr;
  196. }
  197. }
  198. /**
  199. * 将字段填充到指定的长度
  200. *
  201. * @param sourceStr
  202. * String 操作字符串
  203. * @param length
  204. * int 指定长度
  205. * @param withChar
  206. * char 填充的字符
  207. * @param isPadToEnd
  208. * boolean 是否填充在字符的尾部 true :是 ,false:填充在头部
  209. * @return String
  210. */
  211. public static String pad(String sourceStr, int length, char withChar,
  212. boolean isPadToEnd) {
  213. if (sourceStr == null) {
  214. return null;
  215. }
  216. if (sourceStr.length() >= length) {
  217. return sourceStr;
  218. }
  219. StringBuilder sb = new StringBuilder(sourceStr);
  220. for (int i = 0; i < length - sourceStr.length(); i++) {
  221. if (isPadToEnd) {
  222. sb.append(withChar);
  223. } else {
  224. sb.insert(0, withChar);
  225. }
  226. }
  227. return sb.toString();
  228. }
  229. /**
  230. * 数字字符串转化为整数
  231. *
  232. * @param intStr
  233. * String 待转化的数字字符串
  234. * @param intDef
  235. * int 当intStr为空或空字符串时返回的缺省值
  236. * @return int 返回由数字字符串转化成的数字,当intStr为空或空字符串时,返回缺省值intDef
  237. */
  238. public static int getInt(String intStr, int intDef) {
  239. if (null == intStr || "".equals(intStr.trim())) {
  240. return intDef;
  241. }
  242. int intRetu = intDef;
  243. Double db = new Double(intStr);
  244. intRetu = db.intValue();
  245. return intRetu;
  246. }
  247. /**
  248. * 数字字符串转化为整数,在转化时发生异常,则返回0
  249. *
  250. * @param intStr
  251. * String 待转化的数字字符串
  252. * @return int 返回由数字字符串转化成的整数,当intStr为空或空字符串时,返回0
  253. */
  254. public static int getInt(String intStr) {
  255. return getInt(intStr, 0);
  256. }
  257. /**
  258. * 过滤回车换行
  259. *
  260. * @param str
  261. * @return
  262. */
  263. public static String replaceBlank(String str) {
  264. if (str == null || "".equals(str.trim())) {
  265. return "";
  266. }
  267. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  268. Matcher m = p.matcher(str);
  269. String after = m.replaceAll("");
  270. return after;
  271. }
  272. /**
  273. * 将无效的windows文件名字符替换成下划线
  274. *
  275. * @param fileName
  276. * @return
  277. */
  278. public static String replaceInvalidFileNameChars(String fileName) {
  279. String regex = "[\\\\/*?<>:\"|]";
  280. return Pattern.compile(regex).matcher(fileName).replaceAll("_");
  281. }
  282. public static String arrayToString(Object[] array) {
  283. if (array == null)
  284. return "null";
  285. StringBuilder sb = new StringBuilder();
  286. sb.append("[");
  287. boolean first = true;
  288. for (Object obj : array) {
  289. if (first)
  290. first = false;
  291. else
  292. sb.append(",");
  293. sb.append(obj);
  294. }
  295. sb.append("]");
  296. return sb.toString();
  297. }
  298. public static String toString(Object obj) {
  299. if (obj == null) {
  300. return "null";
  301. } else if (obj.getClass().isArray()) {
  302. return arrayToString((Object[]) obj);
  303. } else {
  304. return obj.toString();
  305. }
  306. }
  307. public static int compare(String str1, String str2) {
  308. if (str1 == null) {
  309. if (str2 == null)
  310. return 0;
  311. else
  312. return -1;
  313. } else {
  314. if (str2 == null)
  315. return 1;
  316. else
  317. return str1.compareTo(str2);
  318. }
  319. }
  320. public static interface ReplacementGetter {
  321. String getReplacement(String matchStr);
  322. }
  323. /**
  324. * 字符串替换
  325. *
  326. * @param input
  327. * 要替换的字符串
  328. * @param matchRegex
  329. * 匹配要替换的内容的正则表达式
  330. * @param replacementGetter
  331. * 替换过程中,通过调用此接口查询匹配的原文对应的替换内容
  332. * @return
  333. */
  334. public static StringBuffer replaceString(CharSequence input,
  335. String matchRegex, ReplacementGetter replacementGetter) {
  336. Pattern pattern = Pattern.compile(matchRegex);
  337. StringBuffer buf = new StringBuffer();
  338. Matcher matcher = pattern.matcher(input);
  339. while (matcher.find()) {
  340. String str = matcher.group();
  341. String replacement = replacementGetter.getReplacement(str);
  342. matcher.appendReplacement(buf, replacement);
  343. }
  344. matcher.appendTail(buf);
  345. return buf;
  346. }
  347. public static void main(String args[]) {
  348. System.out.println(startsIgroneCaseWith("abcd", "Ab"));
  349. System.out.println(startsIgroneCaseWith("abcd", "cD"));
  350. System.out.println(startsIgroneCaseWith("abcd", ""));
  351. System.out.println(endsIgroneCaseWith("abcd", "Ab"));
  352. System.out.println(endsIgroneCaseWith("abcd", "cD"));
  353. }
  354. public static List<String> splitToList(String source,String pattern)
  355. {
  356. List<String> tempArray = new ArrayList<String>();
  357. if (!isEmpty(source))
  358. {
  359. String[] tempIds = source.split(pattern);
  360. for (String s : tempIds) {
  361. if (!StringUtils.isBlank(s)) {
  362. tempArray.add(s);
  363. }
  364. }
  365. }
  366. return tempArray;
  367. }
  368. }