guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...
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測試,以確定所提供的字符串中是否存在一組指定字符。
翻譯自: 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确定字符串中字符或整数的存在...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 耳机音响红色和绿色线插哪个孔耳机插孔是绿
- 下一篇: 打造桌面音响院只差最后一步?惠威618超