分割字符串到数组/替换字符串/返回指定字节长度的字符串/判断是否为浮点数,包括double和float/email判断/是否纯汉字判断/空白判断
2013-01-04 22:52
555 查看
package 字符串类; import java.util.*; import java.util.regex.Pattern; public class 某人编写的字符串工具类 { /** * 分割字符串到数组 * * @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 bf = new StringBuffer(""); int index = -1; while ((index = source.indexOf(from)) != -1) { bf.append(source.substring(0, index) + to); source = source.substring(index + from.length()); index = source.indexOf(from); } bf.append(source); return bf.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 ex) { } 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]*$"); return pattern.matcher(str).matches(); } /** * 判断输入的字符串是否符合Email样式. * * @param str 传入的字符串 * @return 是Email样式返回true,否则返回false */ public static boolean isEmail(String str) { Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); return pattern.matcher(str).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; } public static void main(String[] args) { String[] numbers = { "12345", "-12345", "123.45", "-123.45", ".12345", "-.12345", "a12345", "12345a", "123.a45" }; for (String str : numbers) { System.out.println(str + "=" + isInteger(str) + " " + isDouble(str)); } String[] emails = { "1@2.com", "1.2@3.com", "1@3.4.5.com" }; for (String str : emails) { System.out.println(str + "=" + isEmail(str)); } String[] chineses = { "中国", "1中国", "中国1", "1中国2", "中1国" }; for (String str : chineses) { System.out.println(str + "=" + isChinese(str)); } } }
相关文章推荐
- js判断字符串是否为中文,并返回字符串长度
- 华为机试题: 将数组中的字符串按指定长度重新分割(java)
- 写一个函数,用来把一个字符串用指定的字符作为分隔符分割成若干个子串输出 | 写一个函数,用来返回一个字符串中重复出现的最长字串的长度及其开始地址
- 数组的创建/查找数组里面的内容/添加数组中元素/使用指定的字符串把数组链接起来/判断数组内是否有指定的数组元素/四种遍历进行输出数组中的元素有哪些
- NSString:创建,长度,取字符,是否以某字符串开头,结尾, 查找指定字符串的范围,截取,拼接,替换,比较,替换,数值转换,大小写转换
- 判断是否为指定长度内字符串的php函数
- 字符串按照一定长度分割并返回数组!(String按照给定长度分割)
- C 语言 实现 字符串 分割 函数(返回"二维字符数组",及分割后的字符数组的长度)
- 不区分汉字还是英文和数字,按指定字节长度截取字符串
- iOS判断一段字符串长度(汉字2字节)
- Delphi判断字符串中是否包含汉字,并返回汉字位置
- Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组
- ios判断一段字符串长度(汉字2字节)
- 把字符串分割成固定长度的片段,返回数组
- 截取指定长度的字符串(包括汉字)
- 给定一个字符串A,同时给定它的长度n及参数字符数组arg,请返回替换后的字符串
- 字符串工具类(指定字符串的长度和判断是否为空等方法)
- php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)
- 如何判断一个字符串中是否含有汉字或汉字标点(即双字节字符)