如果 String 仅包含 ASCII 字符集则匹配
Match if String contains only ASCII character set
我的问题是我想检查我的 String 是否匹配 ASCII 字符集。
我试图在我的 android 项目中使用 Guava 库。问题是这个库有太多的重量(安装的应用程序大小是 41 MB 并且 Guava 库变成 45MB)。
来自 Guava 库我只需要这个:
CharMatcher.ascii().matchesAllOf();
你有什么想法我应该如何正确检查我的字符串,或者有没有其他轻量级库?
谢谢!
java代码是:
public static boolean isAsciiPrintable(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (isAsciiPrintable(str.charAt(i)) == false) {
return false;
}
}
return true;
}
public static boolean isAsciiPrintable(char ch) {
return ch >= 32 && ch < 127;
}
}
参考:http://www.java2s.com/Code/Java/Data-Type/ChecksifthestringcontainsonlyASCIIprintablecharacters.htm
你可以试试这个:
private static boolean isAllASCII(String input) {
boolean isASCII = true;
for (int i = 0; i < input.length(); i++) {
int c = input.charAt(i);
if (c > 0x7F) {
isASCII = false;
break;
}
}
return isASCII;
}
ref In Java, is it possible to check if a String is only ASCII?
来自RealHowTo's answer to In Java, is it possible to check if a String is only ASCII?
You can do it with java.nio.charset.Charset.
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
public class StringUtils {
static CharsetEncoder asciiEncoder =
Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1
public static boolean isPureAscii(String v) {
return asciiEncoder.canEncode(v);
}
public static void main (String args[])
throws Exception {
String test = "Réal";
System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));
test = "Real";
System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));
/*
* output :
* Réal isPureAscii() : false
* Real isPureAscii() : true
*/
}
}
我的问题是我想检查我的 String 是否匹配 ASCII 字符集。
我试图在我的 android 项目中使用 Guava 库。问题是这个库有太多的重量(安装的应用程序大小是 41 MB 并且 Guava 库变成 45MB)。
来自 Guava 库我只需要这个:
CharMatcher.ascii().matchesAllOf();
你有什么想法我应该如何正确检查我的字符串,或者有没有其他轻量级库?
谢谢!
java代码是:
public static boolean isAsciiPrintable(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (isAsciiPrintable(str.charAt(i)) == false) {
return false;
}
}
return true;
}
public static boolean isAsciiPrintable(char ch) {
return ch >= 32 && ch < 127;
}
}
参考:http://www.java2s.com/Code/Java/Data-Type/ChecksifthestringcontainsonlyASCIIprintablecharacters.htm
你可以试试这个:
private static boolean isAllASCII(String input) {
boolean isASCII = true;
for (int i = 0; i < input.length(); i++) {
int c = input.charAt(i);
if (c > 0x7F) {
isASCII = false;
break;
}
}
return isASCII;
}
ref In Java, is it possible to check if a String is only ASCII?
来自RealHowTo's answer to In Java, is it possible to check if a String is only ASCII?
You can do it with java.nio.charset.Charset.
import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class StringUtils { static CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1 public static boolean isPureAscii(String v) { return asciiEncoder.canEncode(v); } public static void main (String args[]) throws Exception { String test = "Réal"; System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test)); test = "Real"; System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test)); /* * output : * Réal isPureAscii() : false * Real isPureAscii() : true */ } }