String类的一些常用操作方法
- package com.liveyc.framework.util;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.StringTokenizer;
- import org.apache.commons.collections.CollectionUtils;
- /**
- * @author aitf
- * @version 创建时间:2016年12月15日 下午2:38:30
- * 类说明
- */
- public class StringUtil {
- /**
- * <li>判断字符串是否为空值</li>
- * <li>NULL、空格均认为空值</li>.
- *
- * @param value
- * the value
- *
- * @return true, if checks if is empty
- */
- public static boolean isEmpty(String value) {
- return null == value || "".equals(value.trim());
- }
- /**
- * 去除,分隔符,用于金额数值去格式化.
- *
- * @param value
- * the value
- *
- * @return the string
- */
- public static String decimal(String value) {
- if (null == value || "".equals(value.trim())) {
- return "0";
- } else {
- return value.replaceAll(",", "");
- }
- }
- /**
- * 在数组中查找字符串.
- *
- * @param params
- * the params
- * @param name
- * the name
- * @param ignoreCase
- * the ignore case
- *
- * @return the int
- */
- public static int indexOf(String[] params, String name, boolean ignoreCase) {
- if (params == null)
- return -1;
- for (int i = 0, j = params.length; i < j; i++) {
- if (ignoreCase && params[i].equalsIgnoreCase(name)) {
- return i;
- } else if (params[i].equals(name)) {
- return i;
- }
- }
- return -1;
- }
- /**
- * 查询Str2在Str1中出现几次
- *
- * @param str1
- * @param str2
- * @return
- */
- public static int indexAllOf(String str1, String str2) {
- int he = 0;
- for (int i = 0; i < str1.length(); i++) {
- int t = str1.indexOf(str2, i);
- if (i == t) {
- he++;
- }
- }
- return he;
- }
- /**
- * 将字符转数组.
- *
- * @param str
- * the str
- *
- * @return the string[]
- */
- public static String[] toArr(String str) {
- String inStr = str;
- String a[] = null;
- try {
- if (null != inStr) {
- StringTokenizer st = new StringTokenizer(inStr, ",");
- if (st.countTokens() > 0) {
- a = new String[st.countTokens()];
- int i = 0;
- while (st.hasMoreTokens()) {
- a[i++] = st.nextToken();
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return a;
- }
- /**
- * 将字符转数组.
- *
- * @param str
- * the str
- * @param splitChar
- * the split char
- *
- * @return the string[]
- */
- public static String[] toArr(String str, String splitChar) {
- String inStr = str;
- String a[] = null;
- try {
- if (null != inStr) {
- StringTokenizer st = new StringTokenizer(inStr, splitChar);
- if (st.countTokens() > 0) {
- a = new String[st.countTokens()];
- int i = 0;
- while (st.hasMoreTokens()) {
- a[i++] = st.nextToken();
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return a;
- }
- /**
- * 字符串数组包装成字符串.
- *
- * @param ary
- * the ary
- * @param s
- * 包装符号如 ' 或 "
- *
- * @return the string
- */
- public static String toStr(String[] ary, String s) {
- if (ary == null || ary.length < 1)
- return "";
- StringBuffer bf = new StringBuffer();
- bf.append(s);
- bf.append(ary[0]);
- for (int i = 1; i < ary.length; i++) {
- bf.append(s).append(",").append(s);
- bf.append(ary[i]);
- }
- bf.append(s);
- return bf.toString();
- }
- /**
- * 得到字符的编码格式
- *
- * @param str
- * @return
- */
- public static String getEncoding(String str) {
- String encode = "GB2312";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s = encode;
- return s;
- }
- } catch (Exception exception) {
- }
- encode = "ISO-8859-1";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s1 = encode;
- return s1;
- }
- } catch (Exception exception1) {
- }
- encode = "UTF-8";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s2 = encode;
- return s2;
- }
- } catch (Exception exception2) {
- }
- encode = "GBK";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s3 = encode;
- return s3;
- }
- } catch (Exception exception3) {
- }
- return "";
- }
- /**
- * utf8转码 Description :.
- *
- * @param str
- * the str
- *
- * @return the string
- */
- public static String utf8Decoder(String str) {
- try {
- if (str != null) {
- return URLDecoder.decode(str, "UTF-8");
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return str;
- }
- public static String changeCharset(String str, String oldCharset, String newCharset)
- throws UnsupportedEncodingException {
- if (str != null) {
- // 用旧的字符编码解码字符串。解码可能会出现异常。
- byte[] bs = str.getBytes(oldCharset);
- // 用新的字符编码生成字符串
- return new String(bs, newCharset);
- }
- return null;
- }
- /**
- * 过滤掉高亮的html
- * @param str
- * @return
- */
- public static String htmlFilter(String str) {
- if (isEmpty(str)) {
- return str;
- }
- str = str.replace("<font color='red'>", "");
- str = str.replace("<font color='blue'>", "");
- str = str.replace("</font>", "");
- return str;
- }
- public static String trimString(String str){
- if(isEmpty(str)){
- return str;
- }
- return str.trim();
- }
- public static String encodeToUtf(String str) throws Exception {
- if(isEmpty(str)){
- return str;
- }
- return new String(str.getBytes("iso-8859-1"), "UTF-8");
- }
- /**
- * 根据身份证号转性别
- * @param sfzh
- * @return
- */
- public static String converToSex(String sfzh){
- int sex = 0;
- if(StringUtil.isEmpty(sfzh)){
- return "";
- }else{
- if(sfzh.length()==15){
- sex = Integer.parseInt(sfzh.substring(13,14));
- }else if(sfzh.length()==18){
- sex = Integer.parseInt(sfzh.substring(16,17));
- }
- if(sex%2 == 0){
- return "女";
- }else{
- return "男";
- }
- }
- }
- /**
- * 设置地址的Map,并去重
- * @param addrMap
- * @param fromType
- * @param addrs
- */
- public static void setAddr2Map(Map addrMap,String addrs,String fromType){
- String[] addrls = null ;
- if(addrMap==null){
- addrMap = new HashMap();
- }
- if(addrMap.containsKey(fromType)){
- String strAddr = (String)addrMap.get(fromType);
- if(strAddr!=null && strAddr.trim().length()>0){
- addrls = strAddr.split(",");
- }
- if(!isExsit(addrls,addrs)){
- strAddr +=","+addrs;
- addrMap.put(fromType, strAddr);
- }
- }else{
- addrMap.put(fromType, addrs);
- }
- }
- /**
- * 字符口串是否在数据组存在
- * @param addrls
- * @param addrs
- * @return
- */
- private static boolean isExsit(String[] addrls,String addrs){
- if(addrls!=null && addrls.length>0){
- for(int i=0;i<addrls.length;i++){
- if(addrls[i].equals(addrs)){
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 把Map转换成String
- * @param addrMap
- * @return
- */
- public static String convMap2String(Map addrMap){
- StringBuilder tempBuf =new StringBuilder();
- Iterator<Map.Entry> it = addrMap.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String, String> entry = it.next();
- String fldName = entry.getKey();
- String fldValue = entry.getValue();
- tempBuf.append(fldValue).append("(").append(fldName).append(");");
- }
- return tempBuf.toString();
- }
- //字节转换
- public static String formetFileSize(long fileS) {
- DecimalFormat df = new DecimalFormat("#.00");
- String fileSizeString = "";
- if (fileS < 1024) {
- fileSizeString = df.format((long) fileS) + "B";
- } else if (fileS < 1048576) {
- fileSizeString = df.format((long) fileS / 1024) + "KB";
- } else if (fileS < 1073741824) {
- fileSizeString = df.format((long) fileS / 1048576) + "MB";
- } else if(fileS < 1099511627776l) {
- fileSizeString = df.format((long) fileS / 1073741824) + "GB";
- } else{
- fileSizeString = df.format((long) fileS / 1099511627776l) + "TB";
- }
- return fileSizeString;
- }
- public static void main(String[] args) {
- long a = 1948065583104l;
- System.out.println(formetFileSize(a));
- }
- }
String类的一些常用操作方法的更多相关文章
- String 类上的常用操作
java 中String 类上的常用操作: 首先创建对象 String line = new String("String demo"); String line2 = new ...
- string类(二、常用string函数)
常用string相关,参至System.String类: 1/ string.Length a.Length字符串长度 string a="a5"; //a.Length==2 s ...
- String类有哪些常用的方法
String类常用方法 1.String类长度:String没有length的属性,有length()这个方法,可以获取字符串的长度. 可以求得字符串s的长度,但是该长度会包含空格. 2.indexO ...
- String类中一些常用的函数
1 CharAt(index) : 通过他的索引来获取元素 @Test public void test1(){ String a="dfjkdjfd134"; for(int i ...
- Java——String类(常用类)
一.String类——描述字符串 常用的方法简单介绍: 1.charAt() 获取对应位置的字符 2.length() 获取字符串的长度 3.concat() 在字符串的尾部追加内容-----相当于连 ...
- String类常用方法练习
String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串. ...
- 字符串处理总结之一(C#String类)
C#(静态String类) C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的便利.System.String是最常用的字符串操作类,可以帮助开发者完成绝大部分的字 ...
- C#(静态String类)
[转]http://blog.csdn.net/angelazy/article/details/8501776 C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的 ...
- Java基础:String类详解,案例用户登录实现,案例手机号截取实现,案例敏感词替换实现;StringBuilder类详解,StringBuilder和String相互转换,附练习案例.
1.API 1.1 API概述-帮助文档的使用 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK ...
随机推荐
- p2 关节
P2中使用Constraint及其子类表示关节,也就是将两个刚体按照指定的规则约束在一起,形成有规律的.相互限制的运动模拟.P2关节模拟中,两个刚体没有通过任何刚体连接,只是通过算法模拟出关节运动轨迹 ...
- 【Python】Python发展历史
起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加享受计算机带来的乐趣.用他 ...
- SPOJ3713——Primitive Root
终于有一个SPOJ题目是我自己独立做出来的,ORZ,太感动了. 题目意思是给你一个素数,问你一个数r是否满足,r,r^2,r^3,……,r^p-1,全不相同. 以前做过这种类型的题目额.是这样的. 根 ...
- 【poj2409】Let it Bead Polya定理
题目描述 用 $c$ 种颜色去染 $r$ 个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $r·c\le 32$ . 题解 Polya定理 Burnside引理 ...
- Tomcat+JDK安装和配置
Tomcat+JDK安装和配置 一.打开FlashFXP软件,建立连接,选择需要的包,右击传输到 /home/guest中 二.进入到:cd /home/guest中,对tomcat包进行解压 三.将 ...
- 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 ...
- 用python的turtle画图
画5个红色的同心圆代码如下: import turtle turtle.pencolor("red") # 设置画笔的颜色 turtle.pensize() # 设置画笔的宽度 t ...
- 简单的并发服务器(多个线程各自accept)
基于之前讲述的简单循环服务器,做一个多个线程各自accept的服务器demo 由于多个线程各自accept,容易造成数据错误,需要在accept前后枷锁 先看下客户端 客户端创建socket,初始化服 ...
- linux jq命令小结
http://note.youdao.com/noteshare?id=0d84ff04edcaa0be512eb0c1e5c41f47
- Qt ------ 设置透明度
void setWindowOpacity(qreal level); //设置所有控件的不透明度 setAttribute(Qt::WA_TranslucentBackground); // ...