/**
* 字符串操作功能类.
*/
public class StringUtil {
/**
* 字符串替换函数,String的replace函数不能处理'|'符号
*
* @param strSource 被替换的源字符串
* @param strFrom 要查找并替换的子字符串
* @param strTo 要替换为的子字符串
* @return 替换完成的字符串
*/
public static String replace(String strSource, String strFrom, String strTo) {
String strDest = "";
int intFromLen = strFrom.length();
int intPos;
if (strFrom.equals("")) {
return strSource;
}
while ((intPos = strSource.indexOf(strFrom)) != -1) {
strDest = strDest + strSource.substring(0, intPos);
strDest = strDest + strTo;
strSource = strSource.substring(intPos + intFromLen);
}
strDest = strDest + strSource;
return strDest;
}
/**
* 将普通字符串格式化成数据库认可的字符串格式
*
* @param input 要格式化的字符串
* @return 合法的数据库字符串
*/
public static String toSql(String input) {
if (isEmpty(input)) {
return "";
}
else {
return input.replaceAll("'", "''");
}
}
/**
* 截取字符串左侧指定长度的字符串
*
* @param input 输入字符串
* @param count 截取长度
* @return 截取字符串
*/
public static String left(String input, int count) {
if (isEmpty(input)) {
return "";
}
count = (count > input.length()) ? input.length() : count;
return input.substring(0, count);
}
/**
* 截取字符串右侧指定长度的字符串
*
* @param input 输入字符串
* @param count 截取长度
* @return 截取字符串
*/
public static String right(String input, int count) {
if (isEmpty(input)) {
return "";
}
count = (count > input.length()) ? input.length() : count;
return input.substring(input.length() - count, input.length());
}
/**
* 从指定位置开始截取指定长度的字符串
*
* @param input 输入字符串
* @param index 截取位置,左侧第一个字符索引值是1
* @param count 截取长度
* @return 截取字符串
*/
public static String middle(String input, int index, int count) {
if (isEmpty(input)) {
return "";
}
count = (count > input.length() - index + 1) ? input.length() - index + 1 :
count;
return input.substring(index - 1, index + count - 1);
}
/**
* Unicode转换成GBK字符集
*
* @param input 待转换字符串
* @return 转换完成字符串
*/
public static String UnicodeToGB(String input) throws UnsupportedEncodingException {
if (isEmpty(input)) {
return "";
}
else {
String s1;
s1 = new String(input.getBytes("ISO8859_1"), "GBK");
return s1;
}
}
/**
* GBK转换成Unicode字符集
*
* @param input 待转换字符串
* @return 转换完成字符串
*/
public static String GBToUnicode(String input) throws UnsupportedEncodingException {
if (isEmpty(input)) {
return "";
}
else {
String s1;
s1 = new String(input.getBytes("GBK"), "ISO8859_1");
return s1;
}
}
/**
* 分隔字符串成数组.
* <p/>
* 使用StringTokenizer,String的split函数不能处理'|'符号
*
* @param input 输入字符串
* @param delim 分隔符
* @return 分隔后数组
*/
public static String[] splitString(String input, String delim) {
if (isEmpty(input)) {
return new String[0];
}
ArrayList al = new ArrayList();
for (StringTokenizer stringtokenizer = new StringTokenizer(input, delim);
stringtokenizer.hasMoreTokens();
al.add(stringtokenizer.nextToken())) {
}
String result[] = new String[al.size()];
for (int i = 0; i < result.length; i++) {
result[i] = (String) al.get(i);
}
return result;
}
/**
* 判断字符串数组中是否包含某字符串元素
*
* @param substring 某字符串
* @param source 源字符串数组
* @return 包含则返回true,否则返回false
*/
public static boolean isIn(String substring, String[] source) {
if (source == null || source.length == 0) {
return false;
}
for (int i = 0; i < source.length; i++) {
String aSource = source[i];
if (aSource.equals(substring)) {
return true;
}
}
return false;
}
/**
* 判断字符是否为空
*
* @param input 某字符串
* @return 包含则返回true,否则返回false
*/
public static boolean isEmpty(String input) {
return input == null || input.length() == 0;
}
/**
* 获得0-9的随机数
*
* @param length
* @return String
*/
public static String getRandomNumber(int length) {
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(random.nextInt(10));
}
return buffer.toString();
}
/**
* 获得0-9的随机数 长度默认为10
*
* @return String
*/
public static String getRandomNumber() {
return getRandomNumber(10);
}
/**
* 获得0-9,a-z,A-Z范围的随机数
* @param length 随机数长度
* @return String
*/
public static String getRandomChar(int length) {
char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(chr[random.nextInt(62)]);
}
return buffer.toString();
}
public static String getRandomChar() {
return getRandomChar(10);
}
/**
*
* 获得13位当前年月日时秒分随机码13位做位主键
*/
public static String getPrimaryKey() {
Date now = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return dateformat.format(now) + getRandomChar(13);
}
/**
*
*做位主键选者主键长度
*/
public static String getPrimaryKey(int index){
Date now = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String indexAppendZero = DigitalUtil.appendZero(index,4);
return dateformat.format(now) + indexAppendZero + getRandomChar(9-indexAppendZero.length());
}
public static String filterHTML(String input) {
StringBuffer filtered = new StringBuffer();
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '&':
filtered.append("&");
break;
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '\'':
filtered.append("'");
break;
default:
filtered.append(c);
}
}
return (filtered.toString());
}
public static void main(String[] args) {
System.out.println(getPrimaryKey(5));
}
static public String prefixZoreFill(String sourceStr,int len){
int prefix = len - sourceStr.length();
if (prefix<=0) return sourceStr;
for (int i=0;i<prefix;i++ ){
sourceStr = "0" + sourceStr;
}
return sourceStr;
}
static public String replaceAll(String str, String regex, String replacement) {
if (str==null || str.compareTo("")==0 || str.compareTo("null")==0){
return str;
}
if (regex==null || regex.compareTo("null")==0){
return str;
}
if (replacement==null || replacement.compareTo("null")==0){
return str;
}
try{
int iIndex,iFromIndex;
String stmp=new String();;
int iLen = regex.length();
iFromIndex=0;
iIndex=str.indexOf(regex,iFromIndex);
stmp="";
while (iIndex>=0) {
stmp=stmp+str.substring(iFromIndex,iIndex)+replacement;
str=str.substring(iIndex+iLen);
iIndex=str.indexOf(regex,iFromIndex);
}
stmp=stmp+str;
return stmp;
}catch(Exception e){
return str;
}
}
static public int length(String str) {
if (str==null || str.compareTo("")==0 || str.compareTo("null")==0) {
return 0;
}
int enLen=0;
int chLen=0;
char ch=' ';
Character CH=new Character(' ');
int iValue=0;
for(int i=0;i<str.length();i++) {
ch=str.charAt(i);
CH=new Character(ch);
iValue=CH.charValue();
if(iValue<128) {
enLen++;
}else{
chLen++;
}
}
return (enLen + chLen/2);
}
static public String substring(String str, int beginIndex, int endIndex) {
if (str==null || str.compareTo("")==0 || str.compareTo("null")==0) {
return "";
}
String rtsValue=null;
int enLen=0;
int chLen=0;
char ch=' ';
Character CH=new Character(' ');
int iValue=0;
int iLength=0;
int realBegin=0;
int realEnd=0;
int i=0;
while(iLength<beginIndex) {
ch=str.charAt(i);
CH=new Character(ch);
iValue=CH.charValue();
if(iValue<128) {
enLen++;
}else{
chLen++;
}
iLength=enLen + chLen/2;
i++;
}
realBegin=enLen + chLen;
i=realBegin;
while(iLength<endIndex) {
ch=str.charAt(i);
CH=new Character(ch);
iValue=CH.charValue();
if(iValue<128) {
enLen++;
}else{
chLen++;
}
iLength=enLen + chLen/2;
i++;
}
realEnd=enLen + chLen;
rtsValue=str.substring(realBegin,realEnd);
return rtsValue;
}
/**
* 计算最大编号
*/
static public String max(Object obj){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String strdate = sf.format(new Date());//当前日期20070802
String code="",codemax;
if(obj == null){//数据库没有原始记录
code += strdate+"0001";
}else{
codemax = (String)obj;
codemax = String.valueOf(Integer.parseInt(codemax) + 1);
if(codemax.length() == 1)
code += strdate+"000"+codemax;
if(codemax.length() == 2)
code += strdate+"00"+codemax;
if(codemax.length() == 3)
code += strdate+"0"+codemax;
}
return code;
}
/**
* 转换字符串为日期,格式"yyyy-MM-dd HH:mm:ss"
*
* @param date
* 日期字符串
* @return 返回格式化的日期
*/
public static Date strToTime(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setLenient(false);
return formatter.parse(date);
}
/**
* 取得现在的日期,格式"yyyy-MM-dd HH:mm:ss"
*
* @return 返回格式化的日期字符串
*/
public static String getNow() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date Now = new Date();
return formatter.format(Now);
}
}
分享到:
相关推荐
作业为判断字符串是否为2-重复串及其他字符串操作内容 1. 创建Java项目JavaTeaching2012 2. 创建包cn.qtech.util,在其中新建类RepeatedStringEstimator,该类有两个方法:(1)public boolean estimate(String s),...
Str 字符串操作类 Valid 数据类型验证类 WebConfig Web.Config配置类 ---------------- 公历/农历类 日期格式处理类 文件实用类 上传类 图片处理类 授权类 链接辅助类 分页类 反射辅助类 浏览器辅助类 序列化...
字符串实用类 StringUtil.cs 处理字符串分割,转换,嵌入等方法 类型转换类 TypeParse.cs 各种类型互相转换,如int string bool等 用户实用类 User.cs 登陆,退出时候身份加密或解除方式 验证码类 ValidateImage.cs 图片...
否则,由于必须对字符串进行两次转义,因此使用字符串执行此操作会很繁琐。 基本示例: import r from "regexr" ;const int = / \d + / ;const USD = r `\$ ${ int } (\. ${ int } )?` ; // f.e. $3.45 or $5 (请...
主要功能 对象⇒JSON转换--converter.JSON.java n元⇔n元转换--converter.RadixConverter.java Base64编码--converter.Base64.java ...字符串操作--util.StringUtility.java 类似的搜索--util.SearchE
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------...
(StringUtil.cs) 14. 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 15. 各种输入格式验证辅助类(ValidateUtil.cs) ----------Network-------------- 1. Cookie操作辅助类(CookieManger.cs) ...
JavaUtils A collection of some common tools for developing Java. I do develop these to avoid...字符串处理工具类 src/com.cnblogs.honoka.utils.ToolsUtil.java:常用功能工具类 src/com.cnblogs.honoka.utils.Da
(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------Network-------------- Cookie操作辅助类(CookieManger.cs) FTP操作辅助...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------...
有一种特殊的字符串称为raw字符串,被认为是纯文本,其中的\和{等不具有特殊含义,该类字符串在引号前面加r,下面是一个例子: ${r"/${data}"year""}屏幕输出结果为:/${data}"year" 转义 含义 ...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------Network---...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------Network---...
(StringUtil.cs) 14.获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 15.各种输入格式验证辅助类(ValidateUtil.cs) ----------Network-------------- 1.Cookie操作辅助类(CookieManger.cs) 2....
RandomStringUtils – 用于生成随机的字符串; SerializationUtils – 用于处理对象序列化,提供比一般Java序列化更高级的处理能力; StringEscapeUtils – 用于正确处理转义字符,产生正确的Java、JavaScript、HTML...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------...
常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------...
二十、 正则表达式:其实是用来操作字符串的一些规则★★★☆ 135 二十一、 设计模式★★★★★ 136 设计模式简介 136 单例设计模式:★★★★★ 156 工厂模式★★★★★ 159 抽象工厂模式★★★★★ 163 建造者模式...
6.1 字符串类String 108 实例79 创建字符串类 108 实例80 如何使用charAt()方法计算重复字符 109 实例81 按字母顺序比较大小 110 实例82 首尾相连 111 实例83 字符串间的比较 112 实例84 字符集...