1. package com.liveyc.framework.util;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.URLDecoder;
  5. import java.text.DecimalFormat;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.StringTokenizer;
  12.  
  13. import org.apache.commons.collections.CollectionUtils;
  14.  
  15. /**
  16. * @author aitf
  17. * @version 创建时间:2016年12月15日 下午2:38:30
  18. * 类说明
  19. */
  20. public class StringUtil {
  21.  
  22. /**
  23. * <li>判断字符串是否为空值</li>
  24. * <li>NULL、空格均认为空值</li>.
  25. *
  26. * @param value
  27. * the value
  28. *
  29. * @return true, if checks if is empty
  30. */
  31. public static boolean isEmpty(String value) {
  32. return null == value || "".equals(value.trim());
  33. }
  34.  
  35. /**
  36. * 去除,分隔符,用于金额数值去格式化.
  37. *
  38. * @param value
  39. * the value
  40. *
  41. * @return the string
  42. */
  43. public static String decimal(String value) {
  44. if (null == value || "".equals(value.trim())) {
  45. return "0";
  46. } else {
  47. return value.replaceAll(",", "");
  48. }
  49. }
  50.  
  51. /**
  52. * 在数组中查找字符串.
  53. *
  54. * @param params
  55. * the params
  56. * @param name
  57. * the name
  58. * @param ignoreCase
  59. * the ignore case
  60. *
  61. * @return the int
  62. */
  63. public static int indexOf(String[] params, String name, boolean ignoreCase) {
  64. if (params == null)
  65. return -1;
  66. for (int i = 0, j = params.length; i < j; i++) {
  67. if (ignoreCase && params[i].equalsIgnoreCase(name)) {
  68. return i;
  69. } else if (params[i].equals(name)) {
  70. return i;
  71. }
  72. }
  73. return -1;
  74. }
  75.  
  76. /**
  77. * 查询Str2在Str1中出现几次
  78. *
  79. * @param str1
  80. * @param str2
  81. * @return
  82. */
  83. public static int indexAllOf(String str1, String str2) {
  84. int he = 0;
  85. for (int i = 0; i < str1.length(); i++) {
  86. int t = str1.indexOf(str2, i);
  87. if (i == t) {
  88. he++;
  89. }
  90. }
  91. return he;
  92. }
  93.  
  94. /**
  95. * 将字符转数组.
  96. *
  97. * @param str
  98. * the str
  99. *
  100. * @return the string[]
  101. */
  102. public static String[] toArr(String str) {
  103. String inStr = str;
  104. String a[] = null;
  105. try {
  106. if (null != inStr) {
  107. StringTokenizer st = new StringTokenizer(inStr, ",");
  108. if (st.countTokens() > 0) {
  109. a = new String[st.countTokens()];
  110. int i = 0;
  111. while (st.hasMoreTokens()) {
  112. a[i++] = st.nextToken();
  113. }
  114. }
  115. }
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119. return a;
  120. }
  121.  
  122. /**
  123. * 将字符转数组.
  124. *
  125. * @param str
  126. * the str
  127. * @param splitChar
  128. * the split char
  129. *
  130. * @return the string[]
  131. */
  132. public static String[] toArr(String str, String splitChar) {
  133. String inStr = str;
  134. String a[] = null;
  135. try {
  136. if (null != inStr) {
  137. StringTokenizer st = new StringTokenizer(inStr, splitChar);
  138. if (st.countTokens() > 0) {
  139. a = new String[st.countTokens()];
  140. int i = 0;
  141. while (st.hasMoreTokens()) {
  142. a[i++] = st.nextToken();
  143. }
  144. }
  145. }
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. return a;
  150. }
  151.  
  152. /**
  153. * 字符串数组包装成字符串.
  154. *
  155. * @param ary
  156. * the ary
  157. * @param s
  158. * 包装符号如 ' 或 "
  159. *
  160. * @return the string
  161. */
  162. public static String toStr(String[] ary, String s) {
  163. if (ary == null || ary.length < 1)
  164. return "";
  165. StringBuffer bf = new StringBuffer();
  166. bf.append(s);
  167. bf.append(ary[0]);
  168. for (int i = 1; i < ary.length; i++) {
  169. bf.append(s).append(",").append(s);
  170. bf.append(ary[i]);
  171. }
  172. bf.append(s);
  173. return bf.toString();
  174. }
  175.  
  176. /**
  177. * 得到字符的编码格式
  178. *
  179. * @param str
  180. * @return
  181. */
  182. public static String getEncoding(String str) {
  183. String encode = "GB2312";
  184. try {
  185. if (str.equals(new String(str.getBytes(encode), encode))) {
  186. String s = encode;
  187. return s;
  188. }
  189. } catch (Exception exception) {
  190. }
  191. encode = "ISO-8859-1";
  192. try {
  193. if (str.equals(new String(str.getBytes(encode), encode))) {
  194. String s1 = encode;
  195. return s1;
  196. }
  197. } catch (Exception exception1) {
  198. }
  199. encode = "UTF-8";
  200. try {
  201. if (str.equals(new String(str.getBytes(encode), encode))) {
  202. String s2 = encode;
  203. return s2;
  204. }
  205. } catch (Exception exception2) {
  206. }
  207. encode = "GBK";
  208. try {
  209. if (str.equals(new String(str.getBytes(encode), encode))) {
  210. String s3 = encode;
  211. return s3;
  212. }
  213. } catch (Exception exception3) {
  214. }
  215. return "";
  216. }
  217.  
  218. /**
  219. * utf8转码 Description :.
  220. *
  221. * @param str
  222. * the str
  223. *
  224. * @return the string
  225. */
  226. public static String utf8Decoder(String str) {
  227. try {
  228. if (str != null) {
  229. return URLDecoder.decode(str, "UTF-8");
  230. }
  231. } catch (UnsupportedEncodingException e) {
  232. e.printStackTrace();
  233. }
  234. return str;
  235. }
  236.  
  237. public static String changeCharset(String str, String oldCharset, String newCharset)
  238. throws UnsupportedEncodingException {
  239. if (str != null) {
  240. // 用旧的字符编码解码字符串。解码可能会出现异常。
  241. byte[] bs = str.getBytes(oldCharset);
  242. // 用新的字符编码生成字符串
  243. return new String(bs, newCharset);
  244. }
  245. return null;
  246. }
  247.  
  248. /**
  249. * 过滤掉高亮的html
  250. * @param str
  251. * @return
  252. */
  253. public static String htmlFilter(String str) {
  254. if (isEmpty(str)) {
  255. return str;
  256. }
  257. str = str.replace("<font color='red'>", "");
  258. str = str.replace("<font color='blue'>", "");
  259. str = str.replace("</font>", "");
  260.  
  261. return str;
  262. }
  263.  
  264. public static String trimString(String str){
  265. if(isEmpty(str)){
  266. return str;
  267. }
  268. return str.trim();
  269. }
  270.  
  271. public static String encodeToUtf(String str) throws Exception {
  272. if(isEmpty(str)){
  273. return str;
  274. }
  275. return new String(str.getBytes("iso-8859-1"), "UTF-8");
  276. }
  277.  
  278. /**
  279. * 根据身份证号转性别
  280. * @param sfzh
  281. * @return
  282. */
  283. public static String converToSex(String sfzh){
  284. int sex = 0;
  285. if(StringUtil.isEmpty(sfzh)){
  286. return "";
  287. }else{
  288. if(sfzh.length()==15){
  289. sex = Integer.parseInt(sfzh.substring(13,14));
  290. }else if(sfzh.length()==18){
  291. sex = Integer.parseInt(sfzh.substring(16,17));
  292. }
  293. if(sex%2 == 0){
  294. return "女";
  295. }else{
  296. return "男";
  297. }
  298. }
  299. }
  300.  
  301. /**
  302. * 设置地址的Map,并去重
  303. * @param addrMap
  304. * @param fromType
  305. * @param addrs
  306. */
  307. public static void setAddr2Map(Map addrMap,String addrs,String fromType){
  308. String[] addrls = null ;
  309. if(addrMap==null){
  310. addrMap = new HashMap();
  311. }
  312. if(addrMap.containsKey(fromType)){
  313. String strAddr = (String)addrMap.get(fromType);
  314. if(strAddr!=null && strAddr.trim().length()>0){
  315. addrls = strAddr.split(",");
  316. }
  317.  
  318. if(!isExsit(addrls,addrs)){
  319. strAddr +=","+addrs;
  320. addrMap.put(fromType, strAddr);
  321. }
  322. }else{
  323. addrMap.put(fromType, addrs);
  324. }
  325. }
  326.  
  327. /**
  328. * 字符口串是否在数据组存在
  329. * @param addrls
  330. * @param addrs
  331. * @return
  332. */
  333. private static boolean isExsit(String[] addrls,String addrs){
  334. if(addrls!=null && addrls.length>0){
  335. for(int i=0;i<addrls.length;i++){
  336. if(addrls[i].equals(addrs)){
  337. return true;
  338. }
  339. }
  340. }
  341. return false;
  342. }
  343. /**
  344. * 把Map转换成String
  345. * @param addrMap
  346. * @return
  347. */
  348. public static String convMap2String(Map addrMap){
  349. StringBuilder tempBuf =new StringBuilder();
  350. Iterator<Map.Entry> it = addrMap.entrySet().iterator();
  351. while (it.hasNext()) {
  352. Map.Entry<String, String> entry = it.next();
  353. String fldName = entry.getKey();
  354. String fldValue = entry.getValue();
  355. tempBuf.append(fldValue).append("(").append(fldName).append(");");
  356. }
  357. return tempBuf.toString();
  358. }
  359.  
  360. //字节转换
  361. public static String formetFileSize(long fileS) {
  362.  
  363. DecimalFormat df = new DecimalFormat("#.00");
  364. String fileSizeString = "";
  365. if (fileS < 1024) {
  366. fileSizeString = df.format((long) fileS) + "B";
  367. } else if (fileS < 1048576) {
  368. fileSizeString = df.format((long) fileS / 1024) + "KB";
  369. } else if (fileS < 1073741824) {
  370. fileSizeString = df.format((long) fileS / 1048576) + "MB";
  371. } else if(fileS < 1099511627776l) {
  372. fileSizeString = df.format((long) fileS / 1073741824) + "GB";
  373. } else{
  374. fileSizeString = df.format((long) fileS / 1099511627776l) + "TB";
  375. }
  376. return fileSizeString;
  377. }
  378.  
  379. public static void main(String[] args) {
  380.  
  381. long a = 1948065583104l;
  382. System.out.println(formetFileSize(a));
  383. }
  384.  
  385. }

String类的一些常用操作方法的更多相关文章

  1. String 类上的常用操作

    java 中String 类上的常用操作: 首先创建对象  String line = new String("String demo"); String line2 = new ...

  2. string类(二、常用string函数)

    常用string相关,参至System.String类: 1/ string.Length a.Length字符串长度 string a="a5"; //a.Length==2 s ...

  3. String类有哪些常用的方法

    String类常用方法 1.String类长度:String没有length的属性,有length()这个方法,可以获取字符串的长度. 可以求得字符串s的长度,但是该长度会包含空格. 2.indexO ...

  4. String类中一些常用的函数

    1 CharAt(index) : 通过他的索引来获取元素 @Test public void test1(){ String a="dfjkdjfd134"; for(int i ...

  5. Java——String类(常用类)

    一.String类——描述字符串 常用的方法简单介绍: 1.charAt() 获取对应位置的字符 2.length() 获取字符串的长度 3.concat() 在字符串的尾部追加内容-----相当于连 ...

  6. String类常用方法练习

    String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串. ...

  7. 字符串处理总结之一(C#String类)

    C#(静态String类) C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的便利.System.String是最常用的字符串操作类,可以帮助开发者完成绝大部分的字 ...

  8. C#(静态String类)

    [转]http://blog.csdn.net/angelazy/article/details/8501776 C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的 ...

  9. Java基础:String类详解,案例用户登录实现,案例手机号截取实现,案例敏感词替换实现;StringBuilder类详解,StringBuilder和String相互转换,附练习案例.

    1.API 1.1 API概述-帮助文档的使用 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK ...

随机推荐

  1. p2 关节

    P2中使用Constraint及其子类表示关节,也就是将两个刚体按照指定的规则约束在一起,形成有规律的.相互限制的运动模拟.P2关节模拟中,两个刚体没有通过任何刚体连接,只是通过算法模拟出关节运动轨迹 ...

  2. 【Python】Python发展历史

    起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加享受计算机带来的乐趣.用他 ...

  3. SPOJ3713——Primitive Root

    终于有一个SPOJ题目是我自己独立做出来的,ORZ,太感动了. 题目意思是给你一个素数,问你一个数r是否满足,r,r^2,r^3,……,r^p-1,全不相同. 以前做过这种类型的题目额.是这样的. 根 ...

  4. 【poj2409】Let it Bead Polya定理

    题目描述 用 $c$ 种颜色去染 $r$ 个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $r·c\le 32$ . 题解 Polya定理 Burnside引理 ...

  5. Tomcat+JDK安装和配置

    Tomcat+JDK安装和配置 一.打开FlashFXP软件,建立连接,选择需要的包,右击传输到 /home/guest中 二.进入到:cd /home/guest中,对tomcat包进行解压 三.将 ...

  6. Combining HTML5 Web Applications with OpenCV

    The Web Dev Zone is brought to you by Stormpath—offering a pre-built Identity API for developers. Ea ...

  7. 用python的turtle画图

    画5个红色的同心圆代码如下: import turtle turtle.pencolor("red") # 设置画笔的颜色 turtle.pensize() # 设置画笔的宽度 t ...

  8. 简单的并发服务器(多个线程各自accept)

    基于之前讲述的简单循环服务器,做一个多个线程各自accept的服务器demo 由于多个线程各自accept,容易造成数据错误,需要在accept前后枷锁 先看下客户端 客户端创建socket,初始化服 ...

  9. linux jq命令小结

    http://note.youdao.com/noteshare?id=0d84ff04edcaa0be512eb0c1e5c41f47

  10. Qt ------ 设置透明度

    void setWindowOpacity(qreal level);   //设置所有控件的不透明度 setAttribute(Qt::WA_TranslucentBackground);   // ...