package com.sets.speedtest.utils;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.annotation.SuppressLint;
/**
* 字符串工具类
*/
@SuppressLint("DefaultLocale")
public class StringB
{
/** 正则表达式 - 数字 **/
public static final String EXP_NUM = "[^0-9]";
public static final String CHARSET_UTF8 = "UTF-8";
public static final String SPACE = " "; // 空格
public static final String BRACKET_LEFT = "("; // 左括号
public static final String BRACKET_RIGHT = ")"; // 右括号
public static final String EMPTY = ""; // 空
/**
* 去除空格,null时返回null
*
* @param str
* @return
*/
public static String trim(String str)
{
return str == null ? null : str.trim();
}
/**
* 去除空格,null时返回""
*
* @param str
* @return
*/
public static String trimToEmpty(String str)
{
return str == null ? EMPTY : str.trim();
}
// 是否非空
public static boolean isNotBlank(String str)
{
str = trim(str);
if (str != null && !"".equals(str))
{
return true;
}
return false;
}
// 是否为空
public static boolean isBlank(String str)
{
str = trim(str);
if (str == null || "".equals(str))
{
return true;
}
return false;
}
/**
* 屏蔽表达式之外的字符
*
* @param str
* @return
*/
public static String stringFilter(String exp, String str)
{
Pattern p = Pattern.compile(exp);
Matcher m = p.matcher(str);
return m.replaceAll("");
}
/** 返回字符串的字节长度 */
public static int length(String strExp)
{
return strExp.getBytes().length;
}
/**
* 此函数判断字符串是否为NULL,如果为空返回"",否则返回原值
*
* @param sourceStr
* @return
*/
public static String nulltoBlank(Object sourceStr)
{
if (sourceStr == null)
{
return "";
} else
{
return ((String)sourceStr).trim();
}
}
/**
* 此函数判断字符串是否为NULL,如果为空返回defValue,否则返回原值
*
* @param sourceStr
* @param defValue
* @return
*/
public static String nulltoDef(String sourceStr, String defValue)
{
if (sourceStr == null || "".equals((sourceStr = sourceStr.trim())))
{
return defValue;
} else
{
return sourceStr;
}
}
/**
* url Encode
*
* @param data
* @return
*/
public static String encodeParam(String data)
{
try
{
return URLEncoder.encode(data, CHARSET_UTF8);
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return data;
}
/**
* 使用char数组获得字符串
*
* @param strChar
* @return
*/
public static String getStr(char[] strChar)
{
return new String(strChar);
}
/**
* 获得字符指定长度的字符串
*
* @param strChar
* @param len
* @return
*/
public static String getStr(char strChar, int len)
{
char[] temp = new char[len];
for (int i = 0; i < temp.length; i++)
{
temp[i] = strChar;
}
return new String(temp);
}
/**
* 根据字符串字节长度
*
* @param content
* 内容
* @param format
* 表达式
*/
public static int getStrLen(String str, String format)
{
if (StringB.isBlank(str))
{
return 0;
}
if (StringB.isBlank(format))
{
return str.getBytes().length;
}
return String.format(format, str).getBytes().length;
}
/**
* 得到普通数字...不使用科学计数法
*
* @param numString
* @return
*/
public static String toNormalNum(String numString)
{
return new BigDecimal(numString).toPlainString();
}
/**
* 如果是整数,直接返回,如果是小数,保留两位小数
*
* @param s
* @return
*/
public static String subNum2Dot(String s)
{
BigDecimal bd = new BigDecimal(s);
if (bd.longValue() == bd.floatValue())
{
return s;
} else
{
return String.format("%1$.2f", Float.parseFloat(s));
}
}
/**
* a除b获得百分比
*
* @param a
* @param b
* @return
*/
public static String getPersent(int a, int b)
{
Double percent = ((double) a / b);
DecimalFormat decimalFormat = new DecimalFormat("###%");
return decimalFormat.format(percent);
}
/**
* 获得数组索引的值
*
* @param array
* 数组
* @param index
* 索引
* @return
*/
public static String getArrayVal(String[] array, int index)
{
if (array.length > index)
{
return array[index];
}
return null;
}
/**
* Object转int
*
* @param v
* @param defV
* @return
*/
public static int getIntVal(String v, int defV)
{
if (StringB.isBlank(v))
{
return defV;
}
return Integer.parseInt(v);
}
public static void main(String[] args)
{
// System.out.println("date=" + replaceFirst("2002/12/12", "/", "w"));
}
}