java StringUtil 字符串工具类

import java.util.ArrayList;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class StringUtil {
 
@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]);
}
 
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();
}
 
public static String htmlencode(String str) {
if (str == null) {
return null;
}
return replace(""", """, replace("<", "<", str));
}
 
public static String htmldecode(String str) {
if (str == null) {
return null;
}
 
return replace(""", """, replace("<", "<", str));
}
 
private static final String _BR = "
";
 
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;
}
 
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();
}
 
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
return pattern.matcher(str).matches();
}
 
public static boolean isDouble(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
return pattern.matcher(str).matches();
}
 
public static boolean isLetter(String str) {
if (str == null || str.length() < 0) {
return false;
}
Pattern pattern = Pattern.compile("[\\w\\.-_]*");
return pattern.matcher(str).matches();
}
 
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;
}
 
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();
}
 
public static boolean isChinese(String str) {
Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
return pattern.matcher(str).matches();
}
 
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
 
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;
}
 
public static String hangeToBig(String str) {
double value;
try {
value = Double.parseDouble(str.trim());
} catch (Exception e) {
return null;
}
char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
char[] vunit = { '万', '亿' }; // 段名表示
char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
long midVal = (long) (value * 100); // 转化成整形
String valStr = String.valueOf(midVal); // 转化成字符串
 
String head = valStr.substring(0, valStr.length() - 2); // 取整数部分
String rail = valStr.substring(valStr.length() - 2); // 取小数部分
 
String prefix = ""; // 整数部分转化的结果
String suffix = ""; // 小数部分转化的结果
// 处理小数点后面的数
if (rail.equals("00")) { // 如果小数部分为0
suffix = "整";
} else {
suffix = digit[rail.charAt(0) - '0'] + "角"
+ digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
}
// 处理小数点前面的数
char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
char zero = '0'; // 标志'0'表示出现过0
byte zeroSerNum = 0; // 连续出现0的次数
for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字
int idx = (chDig.length - i - 1) % 4; // 取段内位置
int vidx = (chDig.length - i - 1) / 4; // 取段位置
if (chDig[i] == '0') { // 如果当前字符是0
zeroSerNum++; // 连续0次数递增
if (zero == '0') { // 标志
zero = digit[0];
} else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
prefix += vunit[vidx - 1];
zero = '0';
}
continue;
}
zeroSerNum = 0; // 连续0次数清零
if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的
prefix += zero;
zero = '0';
}
prefix += digit[chDig[i] - '0']; // 转化该数字表示
if (idx > 0)
prefix += hunit[idx - 1];
if (idx == 0 && vidx > 0) {
prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿
}
}
 
if (prefix.length() > 0)
prefix += '圆'; // 如果整数部分存在,则有圆的字样
return prefix + suffix; // 返回正确表示
}
 
@SuppressWarnings("unused")
private static String removeSameString(String str) {
Set mLinkedSet = new LinkedHashSet();// set集合的特征:其子集不可以重复
String[] strArray = str.split(" ");// 根据空格(正则表达式)分割字符串
StringBuffer sb = new StringBuffer();
 
for (int i = 0; i < strArray.length; i++) {
if (!mLinkedSet.contains(strArray[i])) {
mLinkedSet.add(strArray[i]);
sb.append(strArray[i] + " ");
}
}
System.out.println(mLinkedSet);
return sb.toString();
}
 
public static String encoding(String src) {
if (src == null)
return "";
StringBuilder result = new StringBuilder();
if (src != null) {
src = src.trim();
for (int pos = 0; pos < src.length(); pos++) {
switch (src.charAt(pos)) {
case '"':
result.append(""");
break;
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '\'':
result.append("'");
break;
case '&':
result.append("&");
break;
case '%':
result.append("&pc;");
break;
case '_':
result.append("&ul;");
break;
case '#':
result.append("&shap;");
break;
case '?':
result.append("&ques;");
break;
default:
result.append(src.charAt(pos));
break;
}
}
}
return result.toString();
}
 
public static boolean isHandset(String handset) {
try {
String regex = "^1[\\d]{10}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(handset);
return matcher.matches();
 
} catch (RuntimeException e) {
return false;
}
}
 
public static String decoding(String src) {
if (src == null)
return "";
String result = src;
result = result.replace(""", """).replace("'", "\'");
result = result.replace("<", "<").replace(">", ">");
result = result.replace("&", "&");
result = result.replace("&pc;", "%").replace("&ul", "_");
result = result.replace("&shap;", "#").replace("&ques", "?");
return result;
}
 
public static void main(String[] args) {
String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
String from = "efg";
String to = "房贺威";
System.out.println("在字符串source中,用to替换from,替换结果为:"
+ replace(from, to, source));
System.out.println("返回指定字节长度的字符串:"
+ toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));
System.out.println("判断是否为整数:" + isInteger("+0"));
System.out.println("判断是否为浮点数,包括double和float:" + isDouble("+0.36"));
System.out.println("判断输入的字符串是否符合Email样式:" +
isEmail("fhwbj@163.com"));
System.out.println("判断输入的字符串是否为纯汉字:" + isChinese("你好!"));
System.out.println("判断输入的数据是否是质数:" + isPrime(12));
System.out.println("人民币转换成大写:" + hangeToBig("10019658"));
System.out.println("去掉字符串中重复的子字符串:" + removeSameString("100 100 9658"));
System.out.println("过滤特殊字符:" + encoding("100"s<>fdsd100 9658"));
System.out.println("判断是不是合法的手机号码:" + isHandset("15981807340"));
 
System.out.println("从字符串中取值Email:" + parse("159818 fwhbj@163.com07340"));
}
}

JAVA String 工具类的更多相关文章

  1. java常用工具类(java技术交流群57388149)

    package com.itjh.javaUtil;   import java.util.ArrayList; import java.util.List;   /** * * String工具类. ...

  2. Java Properties工具类详解

    1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...

  3. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  4. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  5. Java并发工具类 - CountDownLatch

    Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...

  6. MinerUtil.java 爬虫工具类

    MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...

  7. MinerDB.java 数据库工具类

    MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...

  8. String工具类

    String工具类 问题描述 MAVEN依赖 代码成果 问题描述 很多时候我们需要对字符串进行很多固定的操作,而这些操作在JDK/JRE中又没有预置,于是我们想到了apache-commons组件,但 ...

  9. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

随机推荐

  1. 【转】简单十步让你全面理解SQL

    简单十步让你全面理解SQL 很多程序员认为SQL是一头难以驯服的野兽.它是为数不多的声明性语言之一,也因为这样,其展示了完全不同于其他的表现形式.命令式语言. 面向对象语言甚至函数式编程语言(虽然有些 ...

  2. Linux系统下搭建DNS服务器——DNS原理总结

    2017-01-07 整理 DNS原理 域名到IP地址的解析过程 IP地址到域名的反向域名解析过程 抓包分析DNS报文和具体解析过程 DNS服务器搭建和配置 这个东东也是今年博主参见校招的时候被很多公 ...

  3. Android JDK配置使支持Gradle更新,Maven安装

    配置Maven 或执行Gradle更新等相关命令时出现以下错误时要重新配置JDK ERROR: JAVA_HOME is set to an invalid directory.JAVA_HOME = ...

  4. [HMLY]10.深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用

    前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如Retain Circle的问题. 目录 1.Retain ...

  5. Linux下的暴力密码在线破解工具Hydra安装及其组件安装-使用

    Linux下的暴力密码在线破解工具Hydra安装及其组件安装-使用 hydra可以破解: http://www.thc.org/thc-hydra,可支持AFP, Cisco AAA, Cisco a ...

  6. netty入门篇(1)

    上一篇 nio简介  下一篇 netty中级篇(2) 一.为什么选择Netty Netty是最流行的框架之一.健壮性.功能.性能.可定制性和可扩展性在同类框架中首屈一指,因此被大规模使用,例如ROCK ...

  7. 国内首家MR头显公司于CES惊艳亮相

    在刚刚过去的CES2017大会上,我们看到了许多较为优秀的VR产品,而在这里面,有一家名不见经传的中国公司易瞳发布了一款兼具VR和AR功能的头显VMG-MARK.它的外观与联想VR和骁龙VR820等产 ...

  8. ios 状态码

    9001 无网络 9002 url错误 9003 链接超时 9005 json解析错误 9503 503 error

  9. Genymotion下载失败解决方法

    Genymotion下载虚拟机版本时会很慢,而且经常下载失败 解决方法如下: 1.先去选择下载你需要的版本,之后会下载(很慢),或者失败. 2.到C:\Users\yourname\AppData\L ...

  10. Linux网络管理之net-tools VS iproute2

    查看网卡及IP ifconfig ip link [show] --------- ifconfig -a ip addr show 激活和停止网络接口 ifconfig eth0 up/down i ...