commons.lang中常用的工具类


//缩短到某长度,用...结尾.其实就是(substring(str, 0, max-3) + "...")
         //public static String abbreviate(String str,int maxWidth)
         StringUtils.abbreviate( "abcdefg" 6 ); // ---"abc..."
          
         //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
         StringUtils.appendIfMissing( "abc" , "xyz" ); //---"abcxyz"
         StringUtils.appendIfMissingIgnoreCase( "abcXYZ" , "xyz" ); //---"abcXYZ"
          
         //首字母大小写转换
         StringUtils.capitalize( "cat" ); //---"Cat"
         StringUtils.uncapitalize( "Cat" ); //---"cat"
          
         //字符串扩充至指定大小且居中(若扩充大小少于原字符大小则返回原字符,若扩充大小为 负数则为0计算 )
         StringUtils.center( "abcd" 2 ); //--- "abcd"
         StringUtils.center( "ab" , - 1 ); //--- "ab"
         StringUtils.center( "ab" 4 ); //---" ab "
         StringUtils.center( "a" 4 "yz" ); //---"yayz"
         StringUtils.center( "abc" 7 "" ); //---"  abc  "
          
         //去除字符串中的"\n", "\r", or "\r\n"
         StringUtils.chomp( "abc\r\n" ); //---"abc"
          
         //判断一字符串是否包含另一字符串
         StringUtils.contains( "abc" "z" ); //---false
         StringUtils.containsIgnoreCase( "abc" "A" ); //---true
          
         //统计一字符串在另一字符串中出现次数
         StringUtils.countMatches( "abba" "a" ); //---2
          
         //删除字符串中的梭有空格
         StringUtils.deleteWhitespace( "   ab  c  " ); //---"abc"
          
         //比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
         StringUtils.difference( "abcde" "abxyz" ); //---"xyz"
          
         //检查字符串结尾后缀是否匹配
         StringUtils.endsWith( "abcdef" "def" ); //---true
         StringUtils.endsWithIgnoreCase( "ABCDEF" "def" ); //---true
         StringUtils.endsWithAny( "abcxyz" new  String[] { null "xyz" "abc" }); //---true
          
         //检查起始字符串是否匹配
         StringUtils.startsWith( "abcdef" "abc" ); //---true
         StringUtils.startsWithIgnoreCase( "ABCDEF" "abc" ); //---true
         StringUtils.startsWithAny( "abcxyz" new  String[] { null "xyz" "abc" }); //---true
          
         //判断两字符串是否相同
         StringUtils.equals( "abc" "abc" ); //---true
         StringUtils.equalsIgnoreCase( "abc" "ABC" ); //---true
          
         //比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
         StringUtils.getCommonPrefix( new  String[] { "abcde" "abxyz" }); //---"ab"
          
         //正向查找字符在字符串中第一次出现的位置
         StringUtils.indexOf( "aabaabaa" "b" ); //---2
         StringUtils.indexOf( "aabaabaa" "b" 3 ); //---5(从角标3后查找)
         StringUtils.ordinalIndexOf( "aabaabaa" "a" 3 ); //---1(查找第n次出现的位置)
          
         //反向查找字符串第一次出现的位置
         StringUtils.lastIndexOf( "aabaabaa" 'b' ); //---5
         StringUtils.lastIndexOf( "aabaabaa" 'b' 4 ); //---2
         StringUtils.lastOrdinalIndexOf( "aabaabaa" "ab" 2 ); //---1
          
         //判断字符串大写、小写
         StringUtils.isAllUpperCase( "ABC" ); //---true
         StringUtils.isAllLowerCase( "abC" ); //---false
          
         //判断是否为空(注:isBlank与isEmpty 区别)
         StringUtils.isBlank( null );StringUtils.isBlank( "" );StringUtils.isBlank( " " ); //---true
         StringUtils.isNoneBlank( " " "bar" ); //---false
          
         StringUtils.isEmpty( null );StringUtils.isEmpty( "" ); //---true
         StringUtils.isEmpty( " " ); //---false
         StringUtils.isNoneEmpty( " " "bar" ); //---true
          
         //判断字符串数字
         StringUtils.isNumeric( "123" ); //---false
         StringUtils.isNumeric( "12 3" ); //---false (不识别运算符号、小数点、空格……)
         StringUtils.isNumericSpace( "12 3" ); //---true
          
         //数组中加入分隔符号
         //StringUtils.join([1, 2, 3], ';');//---"1;2;3"
          
         //大小写转换
         StringUtils.upperCase( "aBc" ); //---"ABC"
         StringUtils.lowerCase( "aBc" ); //---"abc"
         StringUtils.swapCase( "The dog has a BONE" ); //---"tHE DOG HAS A bone"
          
         //替换字符串内容……(replacePattern、replceOnce)
         StringUtils.replace( "aba" "a" "z" ); //---"zbz"
         StringUtils.overlay( "abcdef" "zz" 2 4 ); //---"abzzef"(指定区域)
         StringUtils.replaceEach( "abcde" new  String[]{ "ab" "d" },
                 new  String[]{ "w" "t" }); //---"wcte"(多组指定替换ab->w,d->t)
          
         //重复字符
         StringUtils.repeat( 'e' 3 ); //---"eee"
          
         //反转字符串
         StringUtils.reverse( "bat" ); //---"tab"
          
         //删除某字符
         StringUtils.remove( "queued" 'u' ); //---"qeed"
          
         //分割字符串
         StringUtils.split( "a..b.c" '.' ); //---["a", "b", "c"]
         StringUtils.split( "ab:cd:ef" ":" 2 ); //---["ab", "cd:ef"]
         StringUtils.splitByWholeSeparator( "ab-!-cd-!-ef" "-!-" 2 ); //---["ab", "cd-!-ef"]
         StringUtils.splitByWholeSeparatorPreserveAllTokens( "ab::cd:ef" ":" ); //-["ab"," ","cd","ef"]
          
         //去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
         StringUtils.strip( " ab c " ); //---"ab c"
         StringUtils.stripToNull( null ); //---null
         StringUtils.stripToEmpty( null ); //---""
          
         //截取字符串
         StringUtils.substring( "abcd" 2 ); //---"cd"
         StringUtils.substring( "abcdef" 2 4 ); //---"cd"
          
         //left、right从左(右)开始截取n位字符
         StringUtils.left( "abc" 2 ); //---"ab"
         StringUtils.right( "abc" 2 ); //---"bc"
         //从第n位开始截取m位字符       n  m
         StringUtils.mid( "abcdefg" 2 4 ); //---"cdef"
          
         StringUtils.substringBefore( "abcba" "b" ); //---"a"
         StringUtils.substringBeforeLast( "abcba" "b" ); //---"abc"
         StringUtils.substringAfter( "abcba" "b" ); //---"cba"
         StringUtils.substringAfterLast( "abcba" "b" ); //---"a"
          
         StringUtils.substringBetween( "tagabctag" "tag" ); //---"abc"
         StringUtils.substringBetween( "yabczyabcz" "y" "z" ); //---"abc"

   

三、其它类简介

    RandomStringUtils:

   

1
2
3
4
5
6
7
         //随机生成n位数数字
         RandomStringUtils.randomNumeric(n);
         //在指定字符串中生成长度为n的随机字符串
         RandomStringUtils.random(n,  "abcdefghijk" );
         //指定从字符或数字中生成随机字符串
         System.out.println(RandomStringUtils.random(n,  true false ));  
         System.out.println(RandomStringUtils.random(n,  false true ));

   

    NumberUtils:

1
2
3
4
5
6
         //从数组中选出最大值
         NumberUtils.max( new  int [] {  1 2 3 4  }); //---4
         //判断字符串是否全是整数
         NumberUtils.isDigits( "153.4" ); //--false
         //判断字符串是否是有效数字
         NumberUtils.isNumber( "0321.1" ); //---false

    ArrayUtils:

   

  

1
2
3
4
5
6
7
8
9
       //创建数组
         String[] array = ArrayUtils.toArray( "1" "2" );
         //判断两个数据是否相等,如果内容相同, 顺序相同 则返回 true
         ArrayUtils.isEquals(arr1,arr2);
         //判断数组中是否包含某一对象
         ArrayUtils.contains(arr,  "33" );
         //二维数组转换成MAP
         Map map = ArrayUtils.toMap( new  String[][] { 
                 "RED" "#FF0000"  }, {  "GREEN" "#00FF00"  }, {  "BLUE" "#0000FF"  } });

   

    DateUtils:

   

1
2
3
4
5
6
         //日期加n天
         DateUtils.addDays( new  Date(), n);
         //判断是否同一天
         DateUtils.isSameDay(date1, date2);
         //字符串时间转换为Date
         DateUtils.parseDate(str, parsePatterns);

SET

SET key value [EX seconds] [PX milliseconds] [NX|XX]

将字符串值 value 关联到 key 。

如果 key 已经持有其他值, SET 就覆写旧值,无视类型。

对于某个原本带有生存时间(TTL)的键来说, 当 SET 命令成功在这个键上执行时, 这个键原有的 TTL 将被清除。

可选参数

从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改:

  • EX second :设置键的过期时间为 second 秒。 SET key value EX second 效果等同于 SETEX key second value 。
  • PX millisecond :设置键的过期时间为 millisecond 毫秒。 SET key value PX millisecond 效果等同于 PSETEX key millisecond value 。
  • NX :只在键不存在时,才对键进行设置操作。 SET key value NX 效果等同于 SETNX key value 。
  • XX :只在键已经存在时,才对键进行设置操作。

因为 SET 命令可以通过参数来实现和 SETNX 、 SETEX 和 PSETEX 三个命令的效果,所以将来的 Redis 版本可能会废弃并最终移除SETNX 、 SETEX 和 PSETEX 这三个命令。

可用版本:
>= 1.0.0
时间复杂度:
O(1)
返回值:

在 Redis 2.6.12 版本以前, SET 命令总是返回 OK 。

从 Redis 2.6.12 版本开始,  SET 在设置操作成功完成时,才返回  OK 。
如果设置了  NX 或者  XX ,但因为条件没达到而造成设置操作未执行,那么命令返回空批量回复(NULL Bulk Reply)。
# 对不存在的键进行设置

redis 127.0.0.1:6379> SET key "value"
OK

redis 127.0.0.1:6379> GET key
"value"


# 对已存在的键进行设置

redis 127.0.0.1:6379> SET key "new-value"
OK

redis 127.0.0.1:6379> GET key
"new-value"


# 使用 EX 选项

redis 127.0.0.1:6379> SET key-with-expire-time "hello" EX 10086
OK

redis 127.0.0.1:6379> GET key-with-expire-time
"hello"

redis 127.0.0.1:6379> TTL key-with-expire-time
(integer) 10069


# 使用 PX 选项

redis 127.0.0.1:6379> SET key-with-pexpire-time "moto" PX 123321
OK

redis 127.0.0.1:6379> GET key-with-pexpire-time
"moto"

redis 127.0.0.1:6379> PTTL key-with-pexpire-time
(integer) 111939


# 使用 NX 选项

redis 127.0.0.1:6379> SET not-exists-key "value" NX
OK      # 键不存在,设置成功

redis 127.0.0.1:6379> GET not-exists-key
"value"

redis 127.0.0.1:6379> SET not-exists-key "new-value" NX
(nil)   # 键已经存在,设置失败

redis 127.0.0.1:6379> GEt not-exists-key
"value" # 维持原值不变


# 使用 XX 选项

redis 127.0.0.1:6379> EXISTS exists-key
(integer) 0

redis 127.0.0.1:6379> SET exists-key "value" XX
(nil)   # 因为键不存在,设置失败

redis 127.0.0.1:6379> SET exists-key "value"
OK      # 先给键设置一个值

redis 127.0.0.1:6379> SET exists-key "new-value" XX
OK      # 设置新值成功

redis 127.0.0.1:6379> GET exists-key
"new-value"


# NX 或 XX 可以和 EX 或者 PX 组合使用

redis 127.0.0.1:6379> SET key-with-expire-and-NX "hello" EX 10086 NX
OK

redis 127.0.0.1:6379> GET key-with-expire-and-NX
"hello"

redis 127.0.0.1:6379> TTL key-with-expire-and-NX
(integer) 10063

redis 127.0.0.1:6379> SET key-with-pexpire-and-XX "old value"
OK

redis 127.0.0.1:6379> SET key-with-pexpire-and-XX "new value" PX 123321
OK

redis 127.0.0.1:6379> GET key-with-pexpire-and-XX
"new value"

redis 127.0.0.1:6379> PTTL key-with-pexpire-and-XX
(integer) 112999


# EX 和 PX 可以同时出现,但后面给出的选项会覆盖前面给出的选项

redis 127.0.0.1:6379> SET key "value" EX 1000 PX 5000000
OK

redis 127.0.0.1:6379> TTL key
(integer) 4993  # 这是 PX 参数设置的值

redis 127.0.0.1:6379> SET another-key "value" PX 5000000 EX 1000
OK

redis 127.0.0.1:6379> TTL another-key
(integer) 997   # 这是 EX 参数设置的值

使用模式

命令 SET resource-name anystring NX EX max-lock-time 是一种在 Redis 中实现锁的简单方法。

客户端执行以上的命令:

  • 如果服务器返回 OK ,那么这个客户端获得锁。
  • 如果服务器返回 NIL ,那么客户端获取锁失败,可以在稍后再重试。

设置的过期时间到达之后,锁将自动释放。

可以通过以下修改,让这个锁实现更健壮:

  • 不使用固定的字符串作为键的值,而是设置一个不可猜测(non-guessable)的长随机字符串,作为口令串(token)。
  • 不使用 DEL 命令来释放锁,而是发送一个 Lua 脚本,这个脚本只在客户端传入的值和键的口令串相匹配时,才对键进行删除。

这两个改动可以防止持有过期锁的客户端误删现有锁的情况出现。

以下是一个简单的解锁脚本示例:

if redis.call("get",KEYS[1]) == ARGV[1]
then
    return redis.call("del",KEYS[1])
else
    return 0
end

这个脚本可以通过 EVAL ...script... 1 resource-name token-value 命令来调用。




猜你喜欢

转载自blog.csdn.net/LQM1991/article/details/77259093
今日推荐