33
This commit is contained in:
@@ -0,0 +1,505 @@
|
||||
package cn.shangyu.gdxzExpert.utils;
|
||||
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class StringUtil {
|
||||
public static String TOPIC;
|
||||
public static boolean IS_PUBLISH_TOPIC = false;
|
||||
public static String ReplaceLike(String Cent, String Like)
|
||||
{
|
||||
// Regex objRegex = new Regex(Cent);
|
||||
// Cent=objRegex.replace(Cent, "<font color=\"red\">" + Like + "</font>");
|
||||
Cent=Cent.replace(Like,"<font color=\"red\">" + Like + "</font>");
|
||||
return Cent;
|
||||
}
|
||||
//只需要将半角字符转换为全角字符
|
||||
public static String ToSBC(String input) {
|
||||
char c[] = input.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (c[i] == ' ') {
|
||||
c[i] = '\u3000';
|
||||
} else if (c[i] < '\177') {
|
||||
c[i] = (char) (c[i] + 65248);
|
||||
}
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
//¥ 获取人民币符号
|
||||
public static String getYen(){
|
||||
return String.valueOf(Html.fromHtml("¥"));
|
||||
}
|
||||
public static String fenGetYuanFormat1(String fen)
|
||||
{
|
||||
if(StringUtil.isEmpty(fen)||Integer.valueOf(fen)<0.0001)
|
||||
{
|
||||
// return getYen()+"0.00";
|
||||
return "免费";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return getYen()+String.format("%.2f",Integer.valueOf(fen)/100.0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public static String fenGetYuanFormat(String fen)
|
||||
{
|
||||
if(StringUtil.isEmpty(fen)||Integer.valueOf(fen)<0.0001)
|
||||
{
|
||||
return getYen()+"0.00";
|
||||
// return "免费";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return getYen()+String.format("%.2f",Integer.valueOf(fen)/100.0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/** 判断字符串是否有值,如果为null或者是空字符串或者只有空格或者为"null"字符串,则返回true,否则则返回false */
|
||||
public static boolean isEmpty(String value) {
|
||||
if (!TextUtils.isEmpty(value) && !"".equalsIgnoreCase(value.trim())
|
||||
&& !"null".equalsIgnoreCase(value.trim())) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** 判断code不为空,不为"null",并且code不等于"1" */
|
||||
public static boolean isOneToCode(String code) {
|
||||
if (!TextUtils.isEmpty(code) && !"null".equalsIgnoreCase(code)
|
||||
&& !"1".equals(code)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断给定字符串是否空白串 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
|
||||
*/
|
||||
public static boolean isBland(CharSequence input) {
|
||||
if (input == null || "".equals(input) || "null".equals(input))
|
||||
return true;
|
||||
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理空字符串
|
||||
*
|
||||
* @param str
|
||||
* @return String
|
||||
*/
|
||||
public static String doEmpty(String str) {
|
||||
return doEmpty(str, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理空字符串
|
||||
*
|
||||
* @param str
|
||||
* @param defaultValue
|
||||
* @return String
|
||||
*/
|
||||
public static String doEmpty(String str, String defaultValue) {
|
||||
if (str == null || str.equalsIgnoreCase("null")
|
||||
|| str.trim().equals("") || str.trim().equals("-请选择-")) {
|
||||
str = defaultValue;
|
||||
} else if (str.startsWith("null")) {
|
||||
str = str.substring(4, str.length());
|
||||
}
|
||||
return str.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请选择
|
||||
*/
|
||||
final static String PLEASE_SELECT = "请选择...";
|
||||
|
||||
public static boolean notEmpty(Object o) {
|
||||
return o != null && !"".equals(o.toString().trim())
|
||||
&& !"null".equalsIgnoreCase(o.toString().trim())
|
||||
&& !"undefined".equalsIgnoreCase(o.toString().trim())
|
||||
&& !PLEASE_SELECT.equals(o.toString().trim());
|
||||
}
|
||||
|
||||
public static boolean empty(Object o) {
|
||||
return o == null || "".equals(o.toString().trim())
|
||||
|| "null".equalsIgnoreCase(o.toString().trim())
|
||||
|| "undefined".equalsIgnoreCase(o.toString().trim())
|
||||
|| PLEASE_SELECT.equals(o.toString().trim());
|
||||
}
|
||||
|
||||
public static boolean num(Object o) {
|
||||
int n = 0;
|
||||
try {
|
||||
n = Integer.parseInt(o.toString().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (n > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean decimal(Object o) {
|
||||
double n = 0;
|
||||
try {
|
||||
n = Double.parseDouble(o.toString().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (n > 0.0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给JID返回用户名
|
||||
*
|
||||
* @param Jid
|
||||
* @return
|
||||
*/
|
||||
public static String getUserNameByJid(String Jid) {
|
||||
if (empty(Jid)) {
|
||||
return null;
|
||||
}
|
||||
if (!Jid.contains("@")) {
|
||||
return Jid;
|
||||
}
|
||||
return Jid.split("@")[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 给用户名返回JID
|
||||
*
|
||||
* @param jidFor
|
||||
* 域名//如ahic.com.cn
|
||||
* @param userName
|
||||
* @return
|
||||
*/
|
||||
public static String getJidByName(String userName, String jidFor) {
|
||||
if (empty(jidFor) || empty(jidFor)) {
|
||||
return null;
|
||||
}
|
||||
return userName + "@" + jidFor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给用户名返回JID,使用默认域名ahic.com.cn
|
||||
*
|
||||
* @param userName
|
||||
* @return
|
||||
*/
|
||||
public static String getJidByName(String userName) {
|
||||
String jidFor = "swinner.com";
|
||||
return getJidByName(userName, jidFor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的时间字符串,返回月 日 时 分 秒
|
||||
*
|
||||
* @param allDate
|
||||
* like "yyyy-MM-dd hh:mm:ss SSS"
|
||||
* @return
|
||||
*/
|
||||
public static String getMonthTomTime(String allDate) {
|
||||
return allDate.substring(5, 19);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的时间字符串,返回月 日 时 分 月到分钟
|
||||
*
|
||||
* @param allDate
|
||||
* like "yyyy-MM-dd hh:mm:ss SSS"
|
||||
* @return
|
||||
*/
|
||||
public static String getMonthTime(String allDate) {
|
||||
return allDate.substring(5, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 将一个字符串转化为输入流
|
||||
*/
|
||||
|
||||
public static InputStream getStringStream(String sInputString) {
|
||||
|
||||
if (sInputString != null && !sInputString.trim().equals("")) {
|
||||
|
||||
try {
|
||||
|
||||
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(
|
||||
sInputString.getBytes());
|
||||
|
||||
return tInputStringStream;
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
ex.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 将一个输入流转化为字符串
|
||||
*/
|
||||
|
||||
public static String getStreamString(InputStream tInputStream) {
|
||||
|
||||
if (tInputStream != null) {
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader tBufferedReader = new BufferedReader(
|
||||
new InputStreamReader(tInputStream));
|
||||
|
||||
StringBuffer tStringBuffer = new StringBuffer();
|
||||
|
||||
String sTempOneLine = new String("");
|
||||
|
||||
while ((sTempOneLine = tBufferedReader.readLine()) != null) {
|
||||
|
||||
tStringBuffer.append(sTempOneLine);
|
||||
|
||||
}
|
||||
|
||||
return tStringBuffer.toString();
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
ex.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* unicode转码
|
||||
*
|
||||
* @param theString
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String decodeUnicode(String theString) {
|
||||
char aChar;
|
||||
int len = theString.length();
|
||||
StringBuffer outBuffer = new StringBuffer(len);
|
||||
for (int x = 0; x < len;) {
|
||||
aChar = theString.charAt(x++);
|
||||
if (aChar == '\\') {
|
||||
aChar = theString.charAt(x++);
|
||||
if (aChar == 'u') {
|
||||
// Read the xxxx
|
||||
int value = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
aChar = theString.charAt(x++);
|
||||
switch (aChar) {
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
value = (value << 4) + aChar - '0';
|
||||
break;
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
case 'd':
|
||||
case 'e':
|
||||
case 'f':
|
||||
value = (value << 4) + 10 + aChar - 'a';
|
||||
break;
|
||||
case 'A':
|
||||
case 'B':
|
||||
case 'C':
|
||||
case 'D':
|
||||
case 'E':
|
||||
case 'F':
|
||||
value = (value << 4) + 10 + aChar - 'A';
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(
|
||||
"Malformed \\uxxxx encoding.");
|
||||
}
|
||||
|
||||
}
|
||||
outBuffer.append((char) value);
|
||||
} else {
|
||||
if (aChar == 't')
|
||||
aChar = '\t';
|
||||
else if (aChar == 'r')
|
||||
aChar = '\r';
|
||||
else if (aChar == 'n')
|
||||
aChar = '\n';
|
||||
else if (aChar == 'f')
|
||||
aChar = '\f';
|
||||
outBuffer.append(aChar);
|
||||
}
|
||||
} else
|
||||
outBuffer.append(aChar);
|
||||
}
|
||||
return outBuffer.toString();
|
||||
}
|
||||
|
||||
/***
|
||||
*
|
||||
* @param input
|
||||
* : 银行卡号,例如"6225880137706868"
|
||||
* @return
|
||||
*/
|
||||
public static String formBankCard(String input) {
|
||||
// String result = input.replaceAll("([\\d]{4})(?=\\d)", "$1 ");
|
||||
String temp = "";
|
||||
int len = input.length();
|
||||
switch (len) {
|
||||
case 16:
|
||||
temp = input.substring(0, 4).concat("********")
|
||||
.concat(input.substring(12));
|
||||
break;
|
||||
case 17:
|
||||
case 18:
|
||||
case 19:
|
||||
temp = input.substring(0, 4).concat("************")
|
||||
.concat(input.substring(16));
|
||||
break;
|
||||
default:
|
||||
String moddle = "";
|
||||
for (int i = 0, j = len - 8; i < j; i++) {
|
||||
moddle += "*";
|
||||
}
|
||||
temp = input.substring(0, 4).concat(moddle)
|
||||
.concat(input.substring(len - 4));
|
||||
break;
|
||||
}
|
||||
String result = temp.replaceAll("([\\d]|[*]){4}(?!$)", "$0 ");
|
||||
return result;
|
||||
}
|
||||
|
||||
/***
|
||||
*
|
||||
* @param input
|
||||
* : 银行卡号,例如"6225880137706868" : 银行卡号,例如"***********06868"
|
||||
* @return
|
||||
*/
|
||||
public static String formBankCardBefore(String input) {
|
||||
// String result = input.replaceAll("([\\d]{4})(?=\\d)", "$1 ");
|
||||
String temp = "";
|
||||
int len = input.length();
|
||||
switch (len) {
|
||||
case 16:
|
||||
temp = input.substring(0, 0).concat("************")
|
||||
.concat(input.substring(12));
|
||||
break;
|
||||
case 17:
|
||||
case 18:
|
||||
case 19:
|
||||
temp = input.substring(0, 0).concat("****************")
|
||||
.concat(input.substring(15));
|
||||
break;
|
||||
default:
|
||||
String moddle = "";
|
||||
for (int i = 0, j = len - 8; i < j; i++) {
|
||||
moddle += "*";
|
||||
}
|
||||
temp = input.substring(0, 4).concat(moddle)
|
||||
.concat(input.substring(len - 4));
|
||||
break;
|
||||
}
|
||||
String result = temp.replaceAll("([\\d]|[*]){4}(?!$)", "$0 ");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getnum() {
|
||||
Random random = new Random();// 定义随机类
|
||||
int result = random.nextInt(9);// 返回[0,9)集合中的整数,注意不包括9
|
||||
return result + 1; // +1后,[0,9)集合变为[1,10)集合,满足要求
|
||||
}
|
||||
|
||||
public static String getRandomString(int length) {
|
||||
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Random random = new Random();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int number = random.nextInt(62);
|
||||
sb.append(str.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* string 分隔
|
||||
* @param content
|
||||
* @param len
|
||||
* @param k 最大分隔k组
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<String> stringArray(String content, int len,int k){ //字符串对象,分割长度
|
||||
int length = content.length();
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
||||
//进行判断,切割的字符串长度是否能够整除切割长度
|
||||
if(length%len != 0){
|
||||
int i=0;
|
||||
for(;i<length/len&&i<k;i++){
|
||||
String newContent = content.substring(i*len, (i+1)*len);//生成新的字符串
|
||||
|
||||
list.add(newContent);//添加至list集合
|
||||
}
|
||||
if(i<k)
|
||||
{
|
||||
list.add(content.substring(length/len*len, length%len+length/len*len));
|
||||
}
|
||||
|
||||
}else{
|
||||
for(int j=0;j<length/len&&j<k;j++){
|
||||
String newContent = content.substring(j*len, (j+1)*len);
|
||||
|
||||
list.add(newContent);
|
||||
}
|
||||
}
|
||||
|
||||
return (ArrayList<String>) list;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user