java-StringUtils

工作容易遇到的:
必须要8位,不够的就拿0去补
System.out.println(StringUtils.leftPad("34", 8, "0"));
// 00000034
又或者你需要在一个数组中加入一个元素,你可以这么做:

把数组里的字符串用:连接
System.out.println(StringUtils.join(new String[]{"cat","dog","carrot","leaf","door"}, ":")

// cat:dog:carrot:leaf:door

如果你需要一个大写字母或者是需要一个字符串中的每个单词第一个字母大写,你可以这么做:

System.out.println(StringUtils.capitaliseAllWords("a sentenced to be capitalised"));
// A Sentenced To Be Capitalised

如果你需要计算一个字母在字符串中出现的个数,你可以使用countMatches方法:
System.out.println(StringUtils.countMatches("Bethany plays with army men", "e"));
// 2

甚至还有计算两字符串之间的Levenshtein-Distance

System.out.println(StringUtils.getLevenshteinDistance("David", "Jakob"));
// 4


少于5位的就用99去补 直到补足5为
System.out.println(StringUtils.leftPad("123", 5, "99"));
//99123



-----------------------------------日期的使用:
Name                                 Format

ISO_DATE_FORMAT                      yyyy-MM-dd"2004-01-02"

ISO_DATE_TIME_ZONE_FORMAT            yyyy-MM-ddZZ"2004-01-02-07:00"

ISO_DATETIME_FORMAT                  yyyy-MM-dd'T'HH:mm:ss"2004-01-02T23:22:12"

ISO_DATETIME_TIME_ZONE_FORMAT        yyyy-MM-dd'T'HH:mm:ssZZ"2004-01-02T21:13:45-07:00"

ISO_TIME_FORMAT                     'T'HH:mm:ss"T04:23:22"

ISO_TIME_NO_T_FORMAT                 HH:mm:ss"05:12:34"

ISO_TIME_NO_T_TIME_ZONE_FORMAT       HH:mm:ssZZ"12:32:22-07:00"

ISO_TIME_TIME_ZONE_FORMAT            'T'HH:mm:ssZZ"T18:23:22-07:00"

SMTP_DATETIME_FORMAT                 EEE, dd MMM yyyy HH:mm:ss Z"Wed, 01 Feb 2004 20:03:01 CST"


比较两个日期或让日期四舍五入
Date date1 = new Date();

System.out.println(DateUtils.MILLIS_IN_SECOND
+ "The time right now is >>" + date1);

Thread.currentThread().sleep(DateUtils.MILLIS_IN_SECOND);

Date date2 = new Date();

System.out.println("Is Same Instant >> "
+ DateUtils.isSameInstant(date1, date2));

String[] dates = { "2005.03.24 11:03:26", "2005-03-24 11:03",
"2005/03/24" };

System.out.println("----88------" + dates.length);

System.out.println("----------");
// Display date in HH:mm:ss format
System.out.println("Now >>"
+ DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(System
.currentTimeMillis()));

System.out.println("Date after rounding >>"
+ DateUtils.round(date1, Calendar.HOUR));

// Truncate the hour
System.out.println("Date after truncation >>"
+ DateUtils.truncate(date1, Calendar.HOUR));

//输出结果:
1000The time right now is >>Thu Sep 08 18:37:32 CST 2011
Is Same Instant >> false
----88------3

一个常用的日期:
FastDateFormat formatter = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

String output = formatter.format( new Date( ) );

System.out.println(output);
==》2011-09-09 10:11:20
2011-09-09 13:19:11
--------------------------------------------------------------
Now >>18:37:33
Date after rounding >>Thu Sep 08 19:00:00 CST 2011
Date after truncation >>Thu Sep 08 18:00:00 CST 2011

-------------------------------
import org.apache.commons.lang.builder.ToStringBuilder;
toString :在属性类加了这个  在其他类直接toString就可以啦

public String toString(){
return ToStringBuilder.reflectionToString(this);
}
--------------------------------------
import java.util.Collections;
import org.apache.commons.lang.builder.CompareToBuilder;

对对象排序:
1、序列化
public class Stu implements Comparable{
private int id;
private String name;
2、重写这个方法:
public int compareTo(Object obj) {
Stu anotherComputer = (Stu)obj;
return new CompareToBuilder().
append(this.id, anotherComputer.id).toComparison();
}
3、ArrayList a = new ArrayList();

a.add(new Stu(234,"aer","sg","fgh"));
a.add(new Stu(456,"dfgd","df","fgh"));
a.add(new Stu(88,"ertr","hgh","gfhf"));
Collections.sort(a);
System.out.println(a);
----------------------------------------------------
检查空字符串:
StringUtils.isBlank(String str);
StringUtils.isNotBlank(String str);

缩写字符串:abbreviate()可以按照目标长度缩减字符串,若小于目标长度,最后三位字符以"..."代替
【注意有三个...】
String test = " This is a test of the abbreviation. "
System.out.println( StringUtils.abbreviate( test, 10 ) );

[Console输出]
This is...

StringUtils.abbreviate("How to abbreviate a string?",9) -> How to...

------------------------------
计算字符串出现频率:
【检测字符串出现频率  countMatches()】


File manuscriptFile = new File( " manuscript.txt " );

Reader reader = new FileReader( manuscriptFile );

StringWriter stringWriter = new StringWriter( );

while ( reader.ready( ) ) { writer.write( reader.read( ) ); }

String manuscript = stringWriter.toString( );

// Convert string to lowercase

manuscript = StringUtils.lowerCase(manuscript);

// count the occurrences of "futility"

int numFutility = StringUtils.countMatches( manuscript, " futility " );

-----------------------------------

判断空字符串,空格及null
       isEmpty()判断是否为空字符串或null
       isNotEmpty()==!isEmpty()
       isBlank()判断是否为空格,空字符串或null
       isNotBlank()==!isBlank()

检测字符串内容
       isNumeric()判断是否只包含0-9
       isAlpha()判断是否只包含字母
       isAlphanumeric()判断是否只包含字母和数字的组合
       isAlphaSpace()判断是否只包含空格和字母

substringBefore()捕获指定字符串之前的内容
       substringAfter()捕获指定字符串之后的内容
       substringBeforeLast()捕获指定字符串最后出现处之前的内容
       substringAfterLast()捕获指定字符串最后出现处之后的内容
StringUtils.substringBetween("[hello,heis]","[","]") ->hello,heis



difference(str1,str2)输出第二个字符串与第一个相差的字符串
StringUtils.difference("word","world") ->ld 

反转字符串和反转句子的单词顺序
reverse()
StringUtils.reverse("I'm heis") ->sieh m'I  
StringUtils.reverseSentence("I'm heis") ->heis I'm 


输出两个字符编辑距离,即一个字符串要转换到另一个字符串需要插入,删除和替换的字符的次数。
getLevenshteinDistance(str1,str2)

StringUtils.getLevenshteinDistance("steve","stereo") ->2 
StringUtils.getLevenshteinDistance("heis","hello") ->3 








你可能感兴趣的:(java,JOIN,StringUtils,leftpad)