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

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

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 contained
   assert StringUtils.containsAny("Inspired by Actual Events", 'd', 'Q');  // true: one contained
   assert !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开发人员中非常受欢迎,因为它们提供的方法超出了SDK开发人员通常需要的SDK。 在本文中,我研究了如何使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值