设计在线官网中国,用什么软件做网站seo好,网站推广方案有哪些,巩义做网站推广最近Reddit上的帖子提出了一个问题#xff1a;“ 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数#xff1f; ”基于问题的标题也被以另一种方式问到#xff0c;“一种检查变量是否包含诸如列表之类的数字的方法或快速方法#xff0c;例如或#xff08;x“ 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数 ”基于问题的标题也被以另一种方式问到“一种检查变量是否包含诸如列表之类的数字的方法或快速方法例如或x2B” 我不知道标准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.containsAnyCharSequencechar…接受零个或多个要测试的char元素以查看是否有任何元素在第一个参数表示的String中。 第二个重载版本StringUtils.containsAnyCharSequenceCharSequence期望第二个参数包含要在第一个参数中搜索的所有潜在字符作为单个字符序列。 以下代码清单演示了如何使用这种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 Guavas CharMatcher. This* class exists to demonstrate Apache Commons Lang StringUtils and Guavas* CharMatcher support for determining if a particular character or set of* characters or integers is contained within a given* * This classs 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 Guavas 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进行简洁而富有表现力的测试以确定提供的字符串中是否存在一组指定字符。 参考 在我们的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