apache工具类有大量减轻对数据处理的成本及效率,为何不用?还去写那些原始的方法?
join的用法:
将源字符数组或集合转为字符串
String[] strAry = {"2010","2011","2012"};
String str1 = StringUtils.join(strAry, ',');
System.out.println(str1); //2010,2011,2012
List<String> strList = new ArrayList<String>();
strList.add("2010");
strList.add("2011");
strList.add("2012");
String str2 = StringUtils.join(strList, ',');
System.out.println(str2); //2010,2011,2012
Set<String> strSet = new HashSet<String>();
strSet.add("2010");
strSet.add("2011");
strSet.add("2012");
String str3 = StringUtils.join(strSet.iterator(), ',');
String str4 = StringUtils.join(strSet.iterator(), "@@");
System.out.println(str3); //2012,2011,2010 注意顺序不同
System.out.println(str4); //2012@@2011@@2010
contains的用法:
判断是否包含在源字符串中
String str1 = "2010,2011,2012";
Boolean boolean1 = StringUtils.contains(str1, "2012");
System.out.println(boolean1); //true
String str2 = "";
Boolean boolean2 = StringUtils.contains(str2, "2012");
System.out.println(boolean2); //false
capitalize
首字母大写
String str1 = "person";
str1 = StringUtils.capitalize(str1);
System.out.println(str1); //Person
StringFormate (非StringUtils包中用法):
参考:http://blog.csdn.net/lonely_fireworks/article/details/7962171/
String CODE_PREFIX = "Attriibute%02d";
for (int i = 1; i <= 5; i++) {
System.out.println(String.format(CODE_PREFIX, i));
// Attriibute01
// Attriibute02
// Attriibute03
// Attriibute04
// Attriibute05
}