StringUtil.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.uas.utils;
  2. import org.springframework.util.StringUtils;
  3. import sun.misc.BASE64Decoder;
  4. import sun.misc.BASE64Encoder;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.HashSet;
  10. import java.util.Random;
  11. import java.util.Set;
  12. import java.util.regex.Pattern;
  13. /**
  14. * 字符串操作工具
  15. *
  16. * @author yingp
  17. *
  18. */
  19. public class StringUtil {
  20. /**
  21. * 查找字符串重复项
  22. *
  23. * @param str
  24. * @param ch
  25. * @return
  26. */
  27. public static String getRepeats(String str, String ch) {
  28. Set<String> set = new HashSet<String>();
  29. String[] datas = str.split(ch);
  30. StringBuffer repeat = new StringBuffer();
  31. for (String s : datas) {
  32. if (s != null && !s.trim().equals("")) {
  33. if (!set.contains(s)) {
  34. set.add(s);
  35. } else {
  36. if (repeat.length() > 0) {
  37. repeat.append(",");
  38. }
  39. repeat.append(s);
  40. }
  41. }
  42. }
  43. return repeat.toString();
  44. }
  45. /**
  46. * 去掉字符串重复项
  47. *
  48. * @param str
  49. * @param ch
  50. * @return
  51. */
  52. public static String deleteRepeats(String str, String ch) {
  53. Set<String> set = new HashSet<String>();
  54. String[] datas = str.split(ch);
  55. StringBuffer repeat = new StringBuffer();
  56. for (String s : datas) {
  57. if (s != null && !s.trim().equals("")) {
  58. if (!set.contains(s)) {
  59. set.add(s);
  60. repeat.append(s + ch);
  61. }
  62. }
  63. }
  64. return repeat.toString().substring(0, repeat.toString().length() - 1);
  65. }
  66. /**
  67. * InputStream转成字符串
  68. *
  69. * @param in
  70. * @return
  71. */
  72. public static String parserInputStream(InputStream in) {
  73. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  74. StringBuffer buffer = new StringBuffer();
  75. String line = null;
  76. try {
  77. while ((line = br.readLine()) != null) {
  78. buffer.append(line);
  79. }
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. return buffer.toString();
  84. }
  85. /**
  86. * 字符串是否在数组里面
  87. */
  88. public static boolean isInArray(String[] objs, String str) {
  89. boolean bool = false;
  90. for (String obj : objs) {
  91. if (obj.equals(str)) {
  92. bool = true;
  93. break;
  94. }
  95. }
  96. return bool;
  97. }
  98. /**
  99. * 判断参数是否为空、空字符串、空白格
  100. *
  101. * @param object
  102. * @return
  103. */
  104. public static boolean hasText(Object object) {
  105. return object == null ? false : StringUtils.hasText(object.toString());
  106. }
  107. /**
  108. * @param object
  109. * @return 字符串
  110. */
  111. public static String valueOf(Object object) {
  112. return (object == null) ? null : object.toString();
  113. }
  114. /**
  115. * 当object为空时,返回nvlValue,否则返回object
  116. *
  117. * @param object
  118. * @param nvlValue
  119. * @return
  120. */
  121. public static String nvl(Object object, String nvlValue) {
  122. return !hasText(object) ? nvlValue : object.toString();
  123. }
  124. /**
  125. * 当object为空时,返回nvlValue,否则返回value
  126. *
  127. * @param object
  128. * @param value
  129. * @param nvlValue
  130. * @return
  131. */
  132. public static String nvl2(Object object, String value, String nvlValue) {
  133. return !hasText(object) ? nvlValue : value;
  134. }
  135. /**
  136. * 类似于String.format的逆运算
  137. *
  138. * @param paramString
  139. * 待解析字符串
  140. * @param pattern
  141. * 表达式
  142. * @return
  143. */
  144. public static String[] parse(String paramString, String pattern) {
  145. String[] patternArray = pattern.split("%s");
  146. int i = 0;
  147. int strLen = paramString.length();
  148. int len = patternArray.length;
  149. int startIndex = 0;
  150. int endIndex = 0;
  151. String macher = null;
  152. String temp = paramString;
  153. String[] macherArray = new String[pattern.endsWith("%s") ? len : (len - 1)];
  154. for (String patternStr : patternArray) {
  155. startIndex += patternStr.length();
  156. if (startIndex == strLen)
  157. break;
  158. temp = paramString.substring(startIndex);
  159. if (i < len - 1)
  160. endIndex = startIndex + temp.indexOf(patternArray[i + 1]);
  161. else
  162. endIndex = paramString.length();
  163. macher = paramString.substring(startIndex, endIndex);
  164. macherArray[i++] = macher;
  165. if (i == macherArray.length)
  166. break;
  167. else
  168. startIndex = endIndex;
  169. }
  170. return macherArray;
  171. }
  172. /**
  173. * 判断是否包含汉字
  174. *
  175. * @param paramString
  176. * @return
  177. */
  178. public static boolean hasChinese(String paramString) {
  179. String regExp = "[\\u4e00-\\u9fa5]";
  180. Pattern p = Pattern.compile(regExp);
  181. return p.matcher(paramString).find();
  182. }
  183. final static char[] numbersAndLettersCharArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  184. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
  185. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
  186. 'V', 'W', 'X', 'Y', 'Z' };
  187. /**
  188. * 产生给定长度的随机字符串
  189. *
  190. * @param length
  191. * @return
  192. */
  193. public static String getRandomString(int length) {
  194. if (length < 1) {
  195. return null;
  196. }
  197. Random randGen = new Random();
  198. char[] randBuffer = new char[length];
  199. for (int i = 0; i < randBuffer.length; i++) {
  200. randBuffer[i] = numbersAndLettersCharArray[randGen.nextInt(71)];
  201. }
  202. return new String(randBuffer);
  203. }
  204. /**
  205. * 按长度分割字符串
  206. *
  207. * @param str
  208. * 原字符串
  209. * @param length
  210. */
  211. public static String[] split(String str, int length) {
  212. int strLen = str.length();
  213. int len = (int) Math.ceil((double) strLen / length);
  214. String[] strArray = new String[len];
  215. for (int i = 0; i < len; i++) {
  216. strArray[i] = str.substring(i * length, i < len - 1 ? (i + 1) * length : strLen);
  217. }
  218. return strArray;
  219. }
  220. /**
  221. * 按长度分割字符串
  222. *
  223. * @param str
  224. * 原字符串
  225. * @param length
  226. * 切割长度
  227. * @param prevStr
  228. * 前置字符串
  229. * @param subStr
  230. * 后置字符串
  231. * @param concatStr
  232. * 连接字符串
  233. * @return
  234. */
  235. public static String splitAndConcat(String str, int length, String prevStr, String subStr, String concatStr) {
  236. int strLen = str.length();
  237. int len = (int) Math.ceil((double) strLen / length);
  238. StringBuffer buffer = new StringBuffer();
  239. for (int i = 0; i < len; i++) {
  240. if (i > 0)
  241. buffer.append(concatStr);
  242. buffer.append(prevStr).append(str.substring(i * length, i < len - 1 ? (i + 1) * length : strLen)).append(subStr);
  243. }
  244. return buffer.toString();
  245. }
  246. /**
  247. * 将String[] 数组转为 String
  248. *
  249. * @param Str
  250. * @return
  251. */
  252. public static String ArraysToString(String[] str) {
  253. StringBuffer sb = new StringBuffer();
  254. for (int i = 0; i < str.length; i++) {
  255. sb.append(str[i]);
  256. }
  257. return sb.toString();
  258. }
  259. /**
  260. * 清除换行空格等
  261. *
  262. * @param str
  263. * @return
  264. */
  265. public static String trimBlankChars(String str) {
  266. if (null != str) {
  267. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  268. return p.matcher(str).replaceAll("");
  269. }
  270. return null;
  271. }
  272. /**
  273. * 字节数组转base64字符串
  274. *
  275. * @param bytes
  276. * @return
  277. */
  278. public static String encodeBase64(byte[] bytes) {
  279. if (null != bytes) {
  280. BASE64Encoder encoder = new BASE64Encoder();
  281. return encoder.encode(bytes);
  282. }
  283. return null;
  284. }
  285. /**
  286. * base64字符串转字节数组
  287. *
  288. * @param data
  289. * @return
  290. */
  291. public static byte[] decodeBase64(String data) {
  292. if (null != data) {
  293. BASE64Decoder decoder = new BASE64Decoder();
  294. try {
  295. return decoder.decodeBuffer(data);
  296. } catch (IOException e) {
  297. e.printStackTrace();
  298. }
  299. }
  300. return null;
  301. }
  302. }