package com.common.string;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 功能描述:关于字符串的一些实用操作
*
* @author Administrator
* @Date Jul 18, 2008
* @Time 2:19:47 PM
* @version 1.0
*/
public class StringUtil {
/**
* 功能描述:分割字符串
*
* @param str
* String 原始字符串
* @param splitsign
* String 分隔符
* @return String[] 分割后的字符串数组
*/
@SuppressWarnings("unchecked")
public static String[] split(String str, String splitsign) {
int index;
if (str == null || splitsign == null) {
return null;
}
ArrayList al = new ArrayList();
while ((index = str.indexOf(splitsign)) != -1) {
al.add(str.substring(0, index));
str = str.substring(index + splitsign.length());
}
al.add(str);
return (String[]) al.toArray(new String[0]);
}
/**
* 功能描述:替换字符串
*
* @param from
* String 原始字符串
* @param to
* String 目标字符串
* @param source
* String 母字符串
* @return String 替换后的字符串
*/
public static String replace(String from, String to, String source) {
if (source == null || from == null || to == null)
return null;
StringBuffer str = new StringBuffer("");
int index = -1;
while ((index = source.indexOf(from)) != -1) {
str.append(source.substring(0, index) + to);
source = source.substring(index + from.length());
index = source.indexOf(from);
}
str.append(source);
return str.toString();
}
/**
* 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
*
* @param str
* String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlencode(String str) {
if (str == null) {
return null;
}
return replace("\"", """, replace("<", "<", str));
}
/**
* 替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
*
* @param str
* String
* @return String
*/
public static String htmldecode(String str) {
if (str == null) {
return null;
}
return replace(""", "\"", replace("<", "<", str));
}
private static final String _BR = "<br/>";
/**
* 功能描述:在页面上直接显示文本内容,替换小于号,空格,回车,TAB
*
* @param str
* String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlshow(String str) {
if (str == null) {
return null;
}
str = replace("<", "<", str);
str = replace(" ", " ", str);
str = replace("\r\n", _BR, str);
str = replace("\n", _BR, str);
str = replace("\t", " ", str);
return str;
}
/**
* 功能描述:返回指定字节长度的字符串
*
* @param str
* String 字符串
* @param length
* int 指定长度
* @return String 返回的字符串
*/
public static String toLength(String str, int length) {
if (str == null) {
return null;
}
if (length <= 0) {
return "";
}
try {
if (str.getBytes("GBK").length <= length) {
return str;
}
} catch (Exception e) {
}
StringBuffer buff = new StringBuffer();
int index = 0;
char c;
length -= 3;
while (length > 0) {
c = str.charAt(index);
if (c < 128) {
length--;
} else {
length--;
length--;
}
buff.append(c);
index++;
}
buff.append("...");
return buff.toString();
}
/**
* 功能描述:判断是否为整数
*
* @param str
* 传入的字符串
* @return 是整数返回true,否则返回false
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
return pattern.matcher(str).matches();
}
/**
* 判断是否为浮点数,包括double和float
*
* @param str
* 传入的字符串
* @return 是浮点数返回true,否则返回false
*/
public static boolean isDouble(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
return pattern.matcher(str).matches();
}
/**
* 判断是不是合法字符 c 要判断的字符
*/
public static boolean isLetter(String str) {
if (str == null || str.length() < 0) {
return false;
}
Pattern pattern = Pattern.compile("[\\w\\.-_]*");
return pattern.matcher(str).matches();
}
/**
* 从指定的字符串中提取Email content 指定的字符串
*
* @param content
* @return
*/
public static String parse(String content) {
String email = null;
if (content == null || content.length() < 1) {
return email;
}
// 找出含有@
int beginPos;
int i;
String token = "@";
String preHalf = "";
String sufHalf = "";
beginPos = content.indexOf(token);
if (beginPos > -1) {
// 前项扫描
String s = null;
i = beginPos;
while (i > 0) {
s = content.substring(i - 1, i);
if (isLetter(s))
preHalf = s + preHalf;
else
break;
i--;
}
// 后项扫描
i = beginPos + 1;
while (i < content.length()) {
s = content.substring(i, i + 1);
if (isLetter(s))
sufHalf = sufHalf + s;
else
break;
i++;
}
// 判断合法性
email = preHalf + "@" + sufHalf;
if (isEmail(email)) {
return email;
}
}
return null;
}
/**
* 功能描述:判断输入的字符串是否符合Email样式.
*
* @param str
* 传入的字符串
* @return 是Email样式返回true,否则返回false
*/
public static boolean isEmail(String email) {
if (email == null || email.length() < 1 || email.length() > 256) {
return false;
}
Pattern pattern = Pattern
.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
return pattern.matcher(email).matches();
}
/**
* 功能描述:判断输入的字符串是否为纯汉字
*
* @param str
* 传入的字符窜
* @return 如果是纯汉字返回true,否则返回false
*/
public static boolean isChinese(String str) {
Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
return pattern.matcher(str).matches();
}
/**
* 功能描述:是否为空白,包括null和""
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
/**
* 功能描述:判断是否为质数
*
* @param x
* @return
*/
public static boolean isPrime(int x) {
if (x <= 7) {
if (x == 2 || x == 3 || x == 5 || x == 7)
return true;
}
int c = 7;
if (x % 2 == 0)
return false;
if (x % 3 == 0)
return false;
if (x % 5 == 0)
return false;
int end = (int) Math.sqrt(x);
while (c <= end) {
if (x % c == 0) {
return false;
}
c += 4;
if (x % c == 0) {
return false;
}
c += 2;
if (x % c == 0) {
return false;
}
c += 4;
if (x % c == 0) {
return false;
}
c += 2;
if (x % c == 0) {
return false;
}
c += 4;
if (x % c == 0) {
return false;
}
c += 6;
if (x % c == 0) {
return false;
}
c += 2;
if (x % c == 0) {
return false;
}
c += 6;
}
return true;
}
/**
* 功能描述:人民币转成大写
*
* @param str
* 数字字符串
* @return String 人民币转换成大写后的字符串
*/
public static String hangeToBig(String str) {
double value;
try {
value = Double.parseDouble(str.trim());
} catch (Exception e) {
return null;
}
char[] hunit = { '拾', '佰',
没有合适的资源?快使用搜索试试~ 我知道了~
Java开发常用工具类(源码)
共8个文件
java:8个
4星 · 超过85%的资源 需积分: 10 19 下载量 43 浏览量
2009-05-14
16:04:35
上传
评论 1
收藏 15KB RAR 举报
温馨提示
Java常用工具开发源码,一些开发项目中可能常用到的工具,实现某些特定的功能!
资源推荐
资源详情
资源评论
收起资源包目录
Java Code.rar (8个子文件)
string
StringUtil.java 14KB
database
ConnectDB.java 3KB
network
simpleServer.java 3KB
simpleClient.java 3KB
time
TimeUtil.java 7KB
DateUtil.java 7KB
Time.java 7KB
file
FileUtil.java 3KB
共 8 条
- 1
资源评论
- 玩家张三2014-04-27比较全,留着以后用。
yanshuihua
- 粉丝: 0
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功