mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-10 03:55:10 +00:00
Added isAsciiPrintable() http://issues.apache.org/bugzilla/show_bug.cgi?id=22489.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137815 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0ca766679c
commit
fd3858e7e1
@ -54,7 +54,7 @@
|
||||
* - changes the case of a String</li>
|
||||
* <li><b>CountMatches</b>
|
||||
* - counts the number of occurrences of one String in another</li>
|
||||
* <li><b>IsAlpha/IsNumeric/IsWhitespace</b>
|
||||
* <li><b>IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable</b>
|
||||
* - checks the characters in a String</li>
|
||||
* <li><b>DefaultString</b>
|
||||
* - protects against a null input String</li>
|
||||
@ -109,8 +109,9 @@
|
||||
* @author Gary Gregory
|
||||
* @author Phil Steitz
|
||||
* @author Al Chou
|
||||
* @author Michael Davey
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.127 2004/02/19 21:31:19 fredrik Exp $
|
||||
* @version $Id: StringUtils.java,v 1.128 2004/02/24 22:31:42 fredrik Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||
@ -3862,6 +3863,44 @@ public static boolean isAlphanumericSpace(String str) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if the string contains only ASCII printable characters.</p>
|
||||
*
|
||||
* <p><code>null</code> will return <code>false</code>.
|
||||
* An empty String ("") will return <code>true</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.isAsciiPrintable(null) = false
|
||||
* StringUtils.isAsciiPrintable("") = true
|
||||
* StringUtils.isAsciiPrintable(" ") = true
|
||||
* StringUtils.isAsciiPrintable("Ceki") = true
|
||||
* StringUtils.isAsciiPrintable("ab2c") = true
|
||||
* StringUtils.isAsciiPrintable("!ab-c~") = true
|
||||
* StringUtils.isAsciiPrintable("\u0020") = true
|
||||
* StringUtils.isAsciiPrintable("\u0021") = true
|
||||
* StringUtils.isAsciiPrintable("\u007e") = true
|
||||
* StringUtils.isAsciiPrintable("\u007f") = false
|
||||
* StringUtils.isAsciiPrintable("Ceki G\u00fclc\u00fc") = false
|
||||
* </pre>
|
||||
*
|
||||
* @param str the string to check, may be null
|
||||
* @return <code>true</code> if every character is in the range
|
||||
* 32 thru 126
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean isAsciiPrintable(String str) {
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
int sz = str.length();
|
||||
for (int i = 0; i < sz; i++) {
|
||||
if (CharUtils.isAsciiPrintable(str.charAt(i)) == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if the String contains only unicode digits.
|
||||
* A decimal point is not a unicode digit and returns false.</p>
|
||||
|
@ -24,7 +24,8 @@
|
||||
* Unit tests {@link org.apache.commons.lang.StringUtils} - Substring methods
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: StringUtilsIsTest.java,v 1.8 2004/02/18 23:06:19 ggregory Exp $
|
||||
* @author Michael Davey
|
||||
* @version $Id: StringUtilsIsTest.java,v 1.9 2004/02/24 22:31:43 fredrik Exp $
|
||||
*/
|
||||
public class StringUtilsIsTest extends TestCase {
|
||||
|
||||
@ -123,6 +124,31 @@ public void testIsAlphanumericSpace() {
|
||||
assertEquals(false, StringUtils.isAlphanumericSpace("hkHKHik*khbkuh"));
|
||||
}
|
||||
|
||||
public void testIsAsciiPrintable_String() {
|
||||
assertEquals(false, StringUtils.isAsciiPrintable(null));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable(""));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable(" "));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("a"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("A"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("1"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("Ceki"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("!ab2c~"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("1000"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("10 00"));
|
||||
assertEquals(false, StringUtils.isAsciiPrintable("10\t00"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("10.00"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("10,00"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("!ab-c~"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("hkHK=Hik6i?UGH_KJgU7.tUJgKJ*GI87GI,kug"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("\u0020"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("\u0021"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("\u007e"));
|
||||
assertEquals(false, StringUtils.isAsciiPrintable("\u007f"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("G?lc?"));
|
||||
assertEquals(true, StringUtils.isAsciiPrintable("=?iso-8859-1?Q?G=FClc=FC?="));
|
||||
assertEquals(false, StringUtils.isAsciiPrintable("G\u00fclc\u00fc"));
|
||||
}
|
||||
|
||||
public void testIsNumeric() {
|
||||
assertEquals(false, StringUtils.isNumeric(null));
|
||||
assertEquals(true, StringUtils.isNumeric(""));
|
||||
|
Loading…
x
Reference in New Issue
Block a user