- 浏览: 300404 次
- 性别:
- 来自: 山西
博客专栏
-
天天编程
浏览量:21525
最新评论
-
变脸小伙:
运用到了场景中,希望接力
StringBuffer源码理解 -
fangsj:
IE9 安全设置 把这个禁用掉了
spring mvc 文件上传+本地预览+一次提交 -
xu-ch:
今天面试,遇到这题,求出了相似度,面试官问我算法原理是什么,悲 ...
计算字符串相似度算法——Levenshtein -
flywangfei:
你是创新工场的么?
计算字符串相似度算法——Levenshtein -
scwuwei:
六点起床比较好
《4点起床-最养生和高效的时间管理》读书笔记
这次不按照前面的介绍了,方法都大同小异,下面就介绍几个有意思一点的方法
1.left,取左边几个字符
同理,有 right方法,有mid方法,取中间几个。实现都是用substring实现
例如:
StringUtils.left("abc", 0) = "" StringUtils.left("abc", 2) = "ab" StringUtils.left("abc", 4) = "abc"
这个可能在字符串判断中用到
public static String left(String str, int len) { if (str == null) { return null; } if (len < 0) { return EMPTY; } if (str.length() <= len) { return str; } //这一句话是实现。 return str.substring(0, len); }
2.substringBefore(str,separator),找到出现separator之前的字符串。
实现就是先找到出现separator的位置,然后切字符串。
3.删除全部空格及空格类似字符
实现就是每个比对,通过的写入char[]中。
public static String deleteWhitespace(String str) {}
4.删除某个字符串
//感觉这个方法写的很干脆 public static String removeStart(String str, String remove) { if (isEmpty(str) || isEmpty(remove)) { return str; } if (str.startsWith(remove)){ return str.substring(remove.length()); } return str; } //直接替换 public static String remove(String str, String remove) { if (isEmpty(str) || isEmpty(remove)) { return str; } return replace(str, remove, EMPTY, -1); }
5.repeat 重复
例子:
StringUtils.repeat(null, 2) = null StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = "" StringUtils.repeat("", "x", 3) = "xxx" StringUtils.repeat("?", ", ", 3) = "?, ?, ?"
//这个就是实现的原理, public static String repeat(char ch, int repeat) { //创建一个重复过后的数组,然后往里写入字符 char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); }
6.首字母大写
public static String capitalize(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } //首字母大写,然后切割后面的 return new StringBuilder(strLen) .append(Character.toTitleCase(str.charAt(0))) .append(str.substring(1)) .toString(); }
另外还有首字母小写的方法uncapitalize(),还有大小写装换的swapCase()。
7.统计字符串中出现某个字符串的次数
//统计出现次数"aaaa"出现"aa"算两次 public static int countMatches(CharSequence str, CharSequence sub) { if (isEmpty(str) || isEmpty(sub)) { return 0; } int count = 0; int idx = 0; //查看是否包含 while ((idx = CharSequenceUtils.indexOf(str, sub, idx)) != INDEX_NOT_FOUND) { //包含的话,位置加上匹配的长度,这样才会有"aaaa"出现"aa"两次,而不是三次。 count++; idx += sub.length(); } return count; }
8.判断是否是数字
public static boolean isNumeric(CharSequence cs) { if (cs == null || cs.length() == 0) { return false; } int sz = cs.length(); for (int i = 0; i < sz; i++) { //比较每一个字符对应的数字是否在一定f范围内 if (Character.isDigit(cs.charAt(i)) == false) { return false; } } return true; }
9.将字符串颠倒
public static String reverse(String str) { if (str == null) { return null; } //这里的实现就是两个字符数组的赋值操作 return new StringBuilder(str).reverse().toString(); }
10.将多余字符变成...
还有可以从中间加入的
例子
StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..."
public static String abbreviate(String str, int maxWidth) { return abbreviate(str, 0, maxWidth); } public static String abbreviate(String str, int offset, int maxWidth) { if (str == null) { return null; } //如果最大字符小于4,就报错,因为一个。就是一个字符 if (maxWidth < 4) { throw new IllegalArgumentException("Minimum abbreviation width is 4"); } if (str.length() <= maxWidth) { return str; } if (offset > str.length()) { offset = str.length(); } if (str.length() - offset < maxWidth - 3) { offset = str.length() - (maxWidth - 3); } final String abrevMarker = "..."; //替换最大字符数的最后三个字符为... if (offset <= 4) { return str.substring(0, maxWidth - 3) + abrevMarker; } //这里限制前面的字符是,前面三个+后面三个,所以不能小于7 if (maxWidth < 7) { throw new IllegalArgumentException("Minimum abbreviation width with offset is 7"); } if (offset + maxWidth - 3 < str.length()) { return abrevMarker + abbreviate(str.substring(offset), maxWidth - 3); } return abrevMarker + str.substring(str.length() - (maxWidth - 3)); }
11.列出两字符串不同的内容(列出第二个参数不同的部分)
//例如: difference("i am a machine", "i am a robot") -> "robot" public static String difference(String str1, String str2) { if (str1 == null) { return str2; } if (str2 == null) { return str1; } //列出str1从开始匹配,到不一样的位置开始,然后将这个位置返回,全部一样就返回-1 int at = indexOfDifference(str1, str2); if (at == INDEX_NOT_FOUND) { return EMPTY; } return str2.substring(at); }
同样还有去相同部分的getCommonPrefix
12.直接带编码的toString
public static String toString(byte[] bytes, String charsetName) throws UnsupportedEncodingException { return charsetName == null ? new String(bytes) : new String(bytes, charsetName); }
13.休息
现在StringUtils就剩下两个地方没看了:
一个是Levenshtein距离
还有一个是内部类InitStripAccents
发表评论
-
fastcgi中的多线程使用
2012-04-06 22:38 137040.背景 在项目中加入了 ... -
crc循环校验原理和实现
2012-03-29 23:33 192801.CRC简介 CRC(cyclical redundanc ... -
TreeMap源码理解
2012-01-31 10:44 01.首先看构造方法 public TreeMap() { ... -
HashMap源码理解
2012-01-30 21:33 1843看看HashMap对应的源码。 1.类、接口关系 ... -
StringUtils源码理解(下)
2012-01-16 15:46 2200本文介绍StringUtils的剩下的两个方法 1. ... -
StringUtils源码理解(上)
2012-01-11 23:08 4744StringUtils 源码,使用的是commons-lang ... -
Properties源码理解
2012-07-05 12:23 3856Properties用来读配置文件 ... -
字符流(一)Reader和Writer源码理解
2011-11-27 20:32 14591.Reader 1.1 继承关系 public ... -
字符流(二)BufferedReader和BufferedWriter源码理解
2011-11-27 20:33 47921.BufferedReader 1.1 继承关系 ... -
DataInputStream和DataOutputStream源码理解
2011-11-17 00:02 43791.FilterInputStream简介 列出主要的内 ... -
InputStream,OutputStream源码理解
2011-11-09 22:50 33691.理解字节流和字符流 按流的形式分: 字节流和字符流。 ... -
File源码理解
2011-11-07 23:55 43311.构造函数 最基本的构 ... -
Thread源码理解
2011-10-23 14:36 43101.首先看一下Runnable接口 ... -
泛型简单回顾
2011-09-06 23:36 1287泛型的简介 1.java引入泛型的好处是安全简单。 2. ... -
LinkedList源码理解
2011-08-31 00:26 1406LinkedList源码 0.首先这个类中的两个变量 ... -
Vector源码理解
2011-08-29 23:44 1507Vector类 1.系统的变量 //记录元素的数组 pr ... -
ArrayList源码理解
2011-08-15 21:02 1696构造方法: ... -
Arrays源码理解
2011-08-15 20:34 13351.equals public static boo ... -
StringBuffer源码理解
2011-06-22 19:39 5653StringBuffer 存储和操作字符串 它所继承实现的类 ...
相关推荐
StringUtils.java StringUtils.java
StringUtils.java 基于commons-lang-2.5.jar提取 附带使用文档StringUtils.doc
StringUtils 源码,使用的是commons-lang3-3.1包。 以下是StringUtils的各项用法StringUtils的各项用法
StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 ...除了构造器,StringUtils 中一共有130多个方法,并且都是 static 的,所以我们可以这样调用 StringUtils.xxx() 下面分别对一些常用方法做简要介绍:
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入...
继承了org.apache.commons.lang3.StringUtils工具类,加入了部分常用方法,使用时直接添加到项目的公共utils下,同时在pom.xml加入依赖: <!-- ...
StringUtils(最新的哦)
关于java的StringUtils类的详细描述及使用方法。
org.apache.commons.lang3.StringUtils的jar包,判断字符串为空
StringUtils jar包,让你更好判断非空字符,对象等等。 记得好评哦
StringUtils函数全集StringUtils函数全集
StringUtils 字符串工具类,基本上是封装的commons.lang3.StringUtils,有需要的可以下载看下
java获取客户端ip(经过多次代理)提示StringUtils cannot be resolved 需要先 import org.apache.commons.lang3.StringUtils; /* 内含 common-lang3.jar commons-lang3-3.9-bin.zip commons-lang3-3.9-src.zip ...
commons-lang3-3.1 StringUtils字符串jar包 org.apache.commons.lang3.StringUtils的jar包