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类的一些常用操作方法的更多相关文章

  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. 奇异值分解(SVD)原理详解及推导(转载)

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有 ...

  2. 【BioCode】根据seq与位点信息截取窗口

    代码说明 sequence24371.txt 以上为所有氨基酸的编号,序列,与位点标记.根据标记为“1”的位点,截取窗口:如下(实验结果): 图示为一个窗口为12的蛋白质片段 2N+1=25: 实现代 ...

  3. MySQL专题 1 分布式部署数据库同步问题 BinLog

    什么是 Binlog MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志, ...

  4. 第202天:js---原型与原型链终极详解

    一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object .Function 是 JS 自带的函数对象.下面举例说明 var o1 = ...

  5. BZOJ 2109 航空管制(拓扑排序+贪心)

    绝世好题啊.. 题意:给出一个DAG,和每个点要求出现在这个DAG里面的拓扑排序的位置<=ti,求出所有可能的拓扑排序里面每个点出现的位置的最小值. 正着做不好做,考虑反着做,建立这个图的反图. ...

  6. Selenium遇到问题unknown error:cannot create default profile directory......

    1.selenium遇到问题unknown error:cannot create default profile directory...... 2.解决方案 问题1:把驱动放入C:\Windows ...

  7. CSS单位-长度

    css中的长度单位有很多,不同的单位在特定的需求下能够有相当不错的表现,随着css3的发布,又有了一些新的单位添加进来,使我们在做前端页面的时候能够有更多的选择,更方便快捷的达到我们预期的效果. 正题 ...

  8. Linux学习笔记二:Ubuntu安装SSH(Secure Shell)服务

    Ubuntu默认是没有安装SSH(Secure Shell)服务,如果想要通过ssh链接到Ubuntu,我们需要手动安装ssh-server. SSH分客户端ssh-client,服务端ssh-ser ...

  9. 【BZOJ2109/2535】【NOI2010】航空管制(贪心)

    [BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...

  10. ueditor上传图片配置

    1 去ueditor文件夹下 找 ueidtors/dialogs/image/image.html -- 配置位置大概如下: 107   utils.domReady(function(){ 108 ...