guava和commons

最近Reddit上的帖子提出了一个问题:“ 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? ”基于问题的标题也被以另一种方式问到,“一种检查变量是否包含诸如列表之类的数字的方法或快速方法,例如或('x',2,'B')?” 我不知道标准SDK库中有任何单个方法可以执行此操作(除了使用精心设计的正则表达式),但是在本文中,我使用Guava的CharMatcher和Apache Common Lang的StringUtils类回答了这些问题。

Java的String类确实有一个包含方法 ,如果一个字符被包含在可用于确定String或者字符的某个明确指定序列中包含的String 。 但是,我不知道在单个可执行语句(不计算正则表达式)中以任何方式询问Java给定的String是否包含任何指定的字符集,而无需包含所有字符或以指定顺序包含它们。 Guava和Apache Commons Lang都提供了针对此问题的机制。

Apache Commons Lang (本文中使用的3.1版 )提供了可轻松完成此请求的重载StringUtils.containsAny方法。 这两个重载版本都希望传递给它们的第一个参数是要测试的String (或更确切地说是CharSequence ),以查看它是否包含给定的字母或整数。 第一个重载版本StringUtils.containsAny(CharSequence,char…)接受零个或多个要测试的char元素,以查看是否有任何元素在第一个参数表示的String中。 第二个重载版本StringUtils.containsAny(CharSequence,CharSequence)期望第二个参数包含要在第一个参数中搜索的所有潜在字符作为单个字符序列。

以下代码清单演示了如何使用这种Apache Commons Lang方法来确定给定的字符串是否包含某些字符。 这三个语句都将通过其断言,因为“受实际事件启发”确实包括“ d”和“ A”,但不包括“ Q”。 因为只需要提供的任何一个字符都返回true,就可以通过true的前两个断言。 第三个断言通过了,因为字符串不包含唯一提供的字母,因此否定断言。

确定字符串包含具有StringUtils的字符

private static void demoStringContainingLetterInStringUtils()
{assert StringUtils.containsAny("Inspired by Actual Events", 'd', 'A');  // true: both containedassert StringUtils.containsAny("Inspired by Actual Events", 'd', 'Q');  // true: one containedassert !StringUtils.containsAny("Inspired by Actual Events", 'Q');      // true: none contained (!)
}

Guava的CharMatcher也可以按照下一个代码清单中所示的类似方式使用。

使用CharMatcher确定字符串包含一个字符

private static void demoStringContainingLetterInGuava()
{assert CharMatcher.anyOf("Inspired by Actual Events").matchesAnyOf(new String(new char[]{'d', 'A'}));assert CharMatcher.anyOf("Inspired by Actual Events").matchesAnyOf(new String (new char[] {'d', 'Q'}));assert !CharMatcher.anyOf("Inspired by Actual Events").matchesAnyOf(new String(new char[]{'Q'}));
}

如果我们特别想确保给定的String / CharSequence中的至少一个字符是数字(整数),但是我们不能保证整个字符串都是数字,该怎么办? 可以在上面应用与Apache Commons Lang的StringUtils相同的方法,唯一的变化是要匹配的字母是数字0到9。这在下一个屏幕快照中显示。

确定字符串包含StringUtils的数字

private static void demoStringContainingNumericDigitInStringUtils()
{assert !StringUtils.containsAny("Inspired by Actual Events", "0123456789");assert StringUtils.containsAny("Inspired by Actual Events 2013", "0123456789");
}

番石榴的CharMatcher具有一种CharMatcher的表达方式,用于表达以下问题:所提供的字符序列是否至少包含一个数字。 这显示在下一个代码清单中。

使用CharMatcher确定字符串包含数字

private static void demoStringContainingNumericDigitInGuava()
{assert !CharMatcher.DIGIT.matchesAnyOf("Inspired by Actual Events");assert CharMatcher.DIGIT.matchesAnyOf("Inspired by Actual Events 2013");
}

CharMatcher.DIGIT提供了一种简洁明了的方法来指定我们要匹配的数字。 幸运的是,为了方便确定字符串是否包含其他类型的字符, CharMatcher提供了许多类似于DIGIT其他公共字段 。

为了完整起见,我在下一个代码清单中包含了包含上述所有示例的单个类。 此类的main()函数可以在Java启动器上设置-enableassertions (或-ea ) 标志的情况下运行,并且无需任何AssertionError即可完成。

StringContainsDemonstrator.java

package dustin.examples.strings;import com.google.common.base.CharMatcher;
import static java.lang.System.out;import org.apache.commons.lang3.StringUtils;/*** Demonstrate Apache Commons Lang StringUtils and Guava's CharMatcher. This* class exists to demonstrate Apache Commons Lang StringUtils and Guava's* CharMatcher support for determining if a particular character or set of* characters or integers is contained within a given* * This class's tests depend on asserts being enabled, so specify the JVM option* -enableassertions (-ea) when running this example.* * @author Dustin*/
public class StringContainsDemonstrator
{private static final String CANDIDATE_STRING = "Inspired by Actual Events";private static final String CANDIDATE_STRING_WITH_NUMERAL = CANDIDATE_STRING + " 2013";private static final char FIRST_CHARACTER = 'd';private static final char SECOND_CHARACTER = 'A';private static final String CHARACTERS = new String(new char[]{FIRST_CHARACTER, SECOND_CHARACTER});private static final char NOT_CONTAINED_CHARACTER = 'Q';private static final String NOT_CONTAINED_CHARACTERS = new String(new char[]{NOT_CONTAINED_CHARACTER});private static final String MIXED_CONTAINED_CHARACTERS = new String (new char[] {FIRST_CHARACTER, NOT_CONTAINED_CHARACTER});private static final String NUMERIC_CHARACTER_SET = "0123456789";private static void demoStringContainingLetterInGuava(){assert CharMatcher.anyOf(CANDIDATE_STRING).matchesAnyOf(CHARACTERS);assert CharMatcher.anyOf(CANDIDATE_STRING).matchesAnyOf(MIXED_CONTAINED_CHARACTERS);assert !CharMatcher.anyOf(CANDIDATE_STRING).matchesAnyOf(NOT_CONTAINED_CHARACTERS);}private static void demoStringContainingNumericDigitInGuava(){assert !CharMatcher.DIGIT.matchesAnyOf(CANDIDATE_STRING);assert CharMatcher.DIGIT.matchesAnyOf(CANDIDATE_STRING_WITH_NUMERAL);}private static void demoStringContainingLetterInStringUtils(){assert StringUtils.containsAny(CANDIDATE_STRING, FIRST_CHARACTER, SECOND_CHARACTER);assert StringUtils.containsAny(CANDIDATE_STRING, FIRST_CHARACTER, NOT_CONTAINED_CHARACTER);assert !StringUtils.containsAny(CANDIDATE_STRING, NOT_CONTAINED_CHARACTER);}private static void demoStringContainingNumericDigitInStringUtils(){assert !StringUtils.containsAny(CANDIDATE_STRING, NUMERIC_CHARACTER_SET);assert StringUtils.containsAny(CANDIDATE_STRING_WITH_NUMERAL, NUMERIC_CHARACTER_SET);}/*** Indicate whether assertions are enabled.* * @return {@code true} if assertions are enabled or {@code false} if*    assertions are not enabled (are disabled).*/private static boolean areAssertionsEnabled(){boolean enabled = false; assert enabled = true;return enabled;}/*** Main function for running methods to demonstrate Apache Commons Lang* StringUtils and Guava's CharMatcher support for determining if a particular* character or set of characters or integers is contained within a given* String.* * @param args the command line arguments Command line arguments; none expected.*/public static void main(String[] args){if (!areAssertionsEnabled()){out.println("This class cannot demonstrate anything without assertions enabled.");out.println("\tPlease re-run with assertions enabled (-ea).");System.exit(-1);}out.println("Beginning demonstrations...");demoStringContainingLetterInGuava();demoStringContainingLetterInStringUtils();demoStringContainingNumericDigitInGuava();demoStringContainingNumericDigitInStringUtils();out.println("...Demonstrations Ended");}
}

Guava和Apache Commons Lang在Java开发人员中非常受欢迎,因为它们提供的方法超出了Java开发人员通常需要的SDK所提供的范围。 在本文中,我研究了如何使用Guava的CharMatcher和Apache Commons Lang的StringUtils进行简洁CharMatcher测试,以确定所提供的字符串中是否存在一组指定字符。

参考: 在我们的Inspired by Actual Events博客中,使用我们的JCG合作伙伴 Dustin Marx的Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在 。

翻译自: https://www.javacodegeeks.com/2014/01/determining-presence-of-characters-or-integers-in-string-with-guava-charmatcher-and-apache-commons-lang-stringutils.html

guava和commons

guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...相关推荐

  1. 使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在

    最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,"一种检查变量是否包含诸如 ...

  2. org.apache.commons.lang.StringUtils

    org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...

  3. org.apache.commons.lang.StringUtils的jar包是什么

    org.apache.commons.lang.StringUtils的jar包是什么 commons-lang-2.5.jar

  4. Apache Commons Lang StringUtils

    因此,认为最好谈论我喜欢的另一个Java库. 它已经存在了一段时间,也许不是最令人兴奋的库,但是它非常有用. 我可能每天都使用它. org.apache.commons.lang.StringUtil ...

  5. apache.commons.lang.StringUtils 使用心得

    apache.commons.lang.StringUtils 使用心得 转载于:https://www.cnblogs.com/qinglizlp/p/5549687.html

  6. org.apache.commons.lang.StringUtils中常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...

  7. org.apache.commons.lang.StringUtils(StringUtils工具类的常用方法)

    工作容易遇到的:  必须要8位,不够的就拿0去补  System.out.println(StringUtils.leftPad("34", 8, "0")); ...

  8. maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils...

    早上新建一个web项目,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是想搞清楚状况.刚刚才发现问题原因,这个结果我也是醉了,太坑爹了. 问题现象: ...

  9. org.springframework.uti包下的StringUtils的使用和org.apache.commons.lang包下StringUtils的使用

    一.org.springframework.util.StringUtils StringUtils常用方法 描述 boolean isEmpty(Object str) 判断字符串是否为空,如果为n ...

最新文章

  1. 如何用数据结构解释计算机系统 常用数据结构
  2. 如何实现一个定时的任务,并且可以自己停止
  3. 武汉首座无人驾驶电动汽车充电站投入使用
  4. .NET里面的Interop太烂了
  5. sqlalchemy mysql配置中怎么设置utf8_sqlalchemy 的设置及使用
  6. JavaEE企业级快速开发平台jeesite4的使用和快速搭建项目
  7. 今日arXiv精选 | 28篇EMNLP 2021最新论文
  8. jar打包 剔除第三方依赖以及它的依赖_为什么Spring Boot的 jar 可以直接运行?
  9. 数据归一化和常用的归一化方法
  10. dart常用正则表达式
  11. PRD:倒推迅游手游加速器APP-需求文档
  12. 从北京回来的年轻人,我该告诉你点什么? 1
  13. VC++6.0 Win32应用程序 如何添加窗体 ------阿冬专栏
  14. coap协议说明及函数使用
  15. python对英语的要求_对英文【对英文英语头条】- 对英文知识点 - 中企动力
  16. vue+mysql实现前端对接数据库
  17. centos 一键卸载Openstack
  18. java 命令 线程栈_JVM调试常用命令——jstack命令与Java线程栈(1)
  19. 英语口语练习:点冰淇淋
  20. EASWeb分录F7窗口隐藏无用列

热门文章

  1. 纪中C组模拟赛总结(2019.7.6)
  2. 二元运算 FFT+分治
  3. 动态规划训练11 [String painter HDU - 2476]
  4. 深入理解Java中的逃逸分析
  5. JavaFX UI控件教程(二十五)之Color Picker
  6. Java_io体系之CharArrayReader、CharArrayWriter简介、走进源码及示例——13
  7. 【JDBC】各版本jar包下载网址及Tomcat下载
  8. 将编号为0和1的两个栈存放于一个数组空间V[m]中。
  9. java正则表达式验证密码_最新密码验证正则表达式
  10. 8.2-指令周期(学习笔记)