org.apache.commons.lang.StringUtils的用法实例

package randomstringutils;


import org.apache.commons.lang.StringUtils;


public class StringUtilsTest {
public static void main(String[] args) {
/* 1.字符串以prefix开始 */
System.out.println("-------------1-----------------");
System.out.println(StringUtils.startsWith("sssdf", ""));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", ""));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", "s"));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", "ss"));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", "sss"));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", "sssdf"));// 结果是:true
System.out.println(StringUtils.startsWith("sssdf", "f"));// 结果是:false
System.out.println(StringUtils.startsWith("sssdf", "sssdf"));// 结果是:true


System.out.println("-------------2-----------------");
/* 2.字符串以prefix开始,不区分大小写 */
System.out.println(StringUtils.startsWithIgnoreCase("sssdf", "Sssdf"));// 结果是:true
System.out.println("-------------3-----------------");
/* 3.字符串以数组中的字符串开始 */
// System.out.println(StringUtils.startsWithAny("aabcde",new
// String[]{"g","f"}));//结果是:false
// System.out.println(StringUtils.startsWithAny("aabcde",new
// String[]{"g","a"}));//结果是:true
// System.out.println("------------------------------");
/* 4.字符串以suffix结束 */
System.out.println(StringUtils.endsWith("aabcde", "d"));// 结果是:false
System.out.println(StringUtils.endsWith("aabcde", "e"));// 结果是:true
System.out.println(StringUtils.endsWithIgnoreCase("", ""));// 结果是:true
System.out.println("-------------4-----------------");
/* 5.替换字符串:把text中的searchString替换成replacement,max是最大替换次数,默认是替换所有 */
System.out.println(StringUtils.replaceOnce("sshhhss", "ss", "p"));// 只替换一次-->结果是:phhhss
System.out.println(StringUtils.replace("sshhhs", "ss", "p"));// 全部替换--->结果是:phhhs
System.out.println(StringUtils.replace("sshhhsshss", "ss", "7777", 2));// max:最大替换次数-->结果是:7777hhh7777hss
System.out.println(StringUtils.replaceChars("sshhhs", "ss", "p"));// 替换所有字符,区别于replace--->结果是:pphhhp而不是pphhhs
System.out.println("--------------5----------------");
/*
* 6.按照数组进行替换,位置要匹配,数组长度要相等;暂时没发现下面replaceEach和replaceEachRepeatedly二者的区别
*/
System.out.println(StringUtils.replaceEach("www.baidu.com",
new String[] { "baidu", "com" },
new String[] { "taobao", "net" }));// 结果是:www.taobao.net
System.out.println(StringUtils.replaceEach("www.baidu,baidu.com",
new String[] { "baidu", "com" },
new String[] { "taobao", "net" }));// 结果是:www.taobao,taobao.net
System.out.println(StringUtils.replaceEachRepeatedly("www.baidu.com",
new String[] { "baidu", "com" },
new String[] { "taobao", "net" }));// 结果是:www.taobao.net
System.out.println(StringUtils.replaceEachRepeatedly(
"www.baidu,baidu.com", new String[] { "baidu", "com" },
new String[] { "taobao", "net" }));// 结果是:www.taobao.net
System.out.println("--------------6----------------");
/* 7.比较两个字符串是否相等,如果两个均为null,则也认为相等 */
System.out.println(StringUtils.equals("", ""));// 结果是true
System.out.println(StringUtils.equals(null, null));// 结果是true
System.out.println(StringUtils.equals("", null));// 结果是false
System.out.println(StringUtils.equals(null, ""));// 结果是false
System.out.println(StringUtils.equalsIgnoreCase("ss", "Ss"));// 不区分大小写--结果是true
System.out.println("--------------7----------------");


/* 8.返回searchChar在字符串中第一次出现的位置,如果searchChar没有在字符串中出现,则返回-1 */
System.out.println(StringUtils.indexOf("sdfsfsfdsf", "4"));/* 结果是-1 */
System.out.println(StringUtils.indexOf("sdfsfsfdsf", "f"));/* 结果是2 */
System.out.println("--------------8----------------");
// 查找searchChar在字符串中最后一次出现的索引*/
System.out.println(StringUtils.lastIndexOf("aFkyk", "k"));// 结果是4
System.out.println(StringUtils.lastIndexOf("aFkyk", ""));// 结果是5
System.out.println("--------------9----------------");
/* 9.找出字符数组searChars第一次出现在字符串中的位置 */
System.out.println(StringUtils.indexOfAny("sdsfhhl0", "f"));// 结果是3
System.out.println(StringUtils.indexOfAny("sdsfhhl0", new String[] {
"f", "0" }));// 结果是3
System.out.println(StringUtils.indexOfAny("sdsfhhl0", new String[] {
"t", "j" }));// 结果是-1
System.out.println(StringUtils.indexOfAny("sdsfhhl0", new String[] {
"t", "j", "" }));// 结果是3
System.out.println(StringUtils.lastIndexOfAny("aFkyk", new String[] {
"aFkyk", "k" }));// 找出字符数组searChars最后一次出现在字符串中的位置--结果是4
System.out.println("--------------10----------------");


/*
* 10.找出字符串中不在字符数组searchars中的第一个字符出现的位置(从0位开始)如果都在,返回-1
*/
System.out.println(StringUtils.indexOfAnyBut("sdsfhhl0", "h"));// 结果是0
System.out.println(StringUtils.indexOfAnyBut("sdsfhhl0", "s"));// 结果是1
System.out.println(StringUtils.indexOfAnyBut("aa", "aa"));// 结果是-1
System.out.println("---------------11---------------");
/* 11.统计参数1和参数2开始部分共有的字符个数 */
System.out.println(StringUtils.indexOfDifference("sdsfdsf", "s"));// 结果是1
System.out.println(StringUtils.indexOfDifference(new String[] {"sdsfdsf", "s" }));// 结果是1
System.out.println("---------------12---------------");
/* 12.去掉参数2在参数1开始部分共有的字符串 */
System.out.println(StringUtils.difference("灌灌灌灌", "灌灌灌灌啊啊"));// 结果是:啊啊
System.out.println("---------------13---------------");
/* 13.查找,不区分大小写,没有找到返回-1 */
// System.out.println(StringUtils.indexOfIgnoreCase("aFabbSSdd","f"));//返回1
// System.out.println(StringUtils.indexOfIgnoreCase("aFabbSSdd","f",2));//从指定位置开始查找,不区分大小写--返回-1
// System.out.println(StringUtils.indexOfIgnoreCase("aFabbSSdd","f",1));//返回1
// System.out.println(StringUtils.lastIndexOfIgnoreCase("",""));
// System.out.println(StringUtils.lastIndexOfIgnoreCase("","",2));


/* 14.截取指定位置的字符串 */
System.out.println(StringUtils.substring("dskabcee", 3));
/* 结果是:abcee */
System.out.println("---------------14---------------");
System.out.println(StringUtils.substring("dskabcee", 3, 5));
/* 结果是:ab */
System.out.println("---------------15---------------");
/* 15.截取指定字符串之前的内容 */
System.out.println(StringUtils.substringBefore("dskeabcee", "e"));
/* 结果是:dskeabce */
System.out.println("----------------16--------------");
System.out.println(StringUtils.substringBeforeLast("dskeabcee", "e"));// 一直找到最后一个指定的字符串
/* 结果是:dskeabce */
System.out.println("----------------17--------------");
System.out.println(StringUtils.substringAfter("dskeabcedeh", ""));
/* 结果是:dskeabcedeh */
System.out.println("----------------18--------------");
System.out.println(StringUtils.substringAfterLast("dskeabcedeh", ""));
/* 结果是: */
System.out.println("-----------------19-------------");
/* 16.截取参数2和参数3中间的字符 */
System.out.println(StringUtils.substringBetween("dskeabcedeh", "ds"));
/* 结果是:null */
System.out.println(StringUtils.substringBetween("dskeabcedeh", "ds",
"e"));
/* 结果是:k */
System.out.println(StringUtils.substringsBetween("dskeabcedeh", "ds",
"e"));// 以数组方式返回参数2和参数3中间的字符串
/* 结果是:[keabce] */
System.out.println("--------------20----------------");
/* 1.分割字符串,可以设定得到数组的长度,但一般情况下不要设定,这样会发生冲突 */
System.out.println(StringUtils.split("y5y,4454,545"));// 默认是按,来分割
System.out.println(StringUtils.split("aaaa#sss", "#"));
/* 结果是:[aaaa,sss] */
System.out.println("---------------21---------------");
System.out.println(StringUtils.split("aaaa#sss#", "#", 2));
System.out.println("---------------22---------------");
/* 2.按不同类型进行分割字符串 */
System.out.println(StringUtils.splitByCharacterType("aa3444张三Bcss"));
/* 结果是:[aa,3444,张三,a,B,css,B] */
System.out.println("---------------23---------------");
System.out.println(StringUtils.splitByCharacterTypeCamelCase(""));
/* 结果是:[aa,3444,张三,a,Bcss,B] */
System.out.println("---------------24---------------");
/* 3.分割字符串,""不会被忽略,可以设置分割字符串的数组长度 */
System.out.println(StringUtils.splitByWholeSeparator("aaaa#sss#", "#"));// ""不会被忽略
/* 结果是:[aaaa,sss,]System.out.println(StringUtils.split结果是:[aaaa,sss] */
System.out.println("----------------25--------------");
System.out.println(StringUtils.splitByWholeSeparator("aaaa#sss#ggg",
"#"));//
/* 结果是:[aaaa,sss,ggg] */
System.out.println("---------------26---------------");
System.out.println(StringUtils
.splitByWholeSeparator("aaaa#sss#ggg", ""));// 按空格分割
/* 结果是:[aaaa#,sss#ggg] */
System.out.println("---------------27---------------");
System.out.println(StringUtils.splitByWholeSeparator("aaaa#sss#", "#",
2));// 2设定返回数组的最大长度
/* 结果是:[aaaa,sss#] */
System.out.println("---------------28---------------");
/* 4.分割字符串,""不会被忽略,可以设置分割字符串的数组长度 */
System.out.println(StringUtils.splitByWholeSeparatorPreserveAllTokens(
"sddssfsfasfsaf", null));
/* 结果是:[sddssf,sfasfsaf,] */
System.out.println("----------------29--------------");
System.out.println(StringUtils.splitByWholeSeparatorPreserveAllTokens(
"sddssfsfasfsaf", ""));
/* 结果是:[sddssf,sfasfsaf,] */
System.out.println("-----------------30-------------");
/* 5.同上 */
System.out.println(StringUtils.splitPreserveAllTokens(""));
System.out.println(StringUtils.splitPreserveAllTokens("", ""));
System.out.println(StringUtils.splitPreserveAllTokens("", "", 3));
}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.Clas sUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值