Java 如果字符串仅包含ASCII字符集,则匹配
我的问题是,我想检查我的字符串是否与ASCII字符集匹配 我试图在我的android项目中使用这个库。问题是该库的重量太大(已安装的应用程序大小为41MB,而使用Guava库则变为45MB) 从番石榴图书馆,我只需要这个:Java 如果字符串仅包含ASCII字符集,则匹配,java,android,string,ascii,guava,Java,Android,String,Ascii,Guava,我的问题是,我想检查我的字符串是否与ASCII字符集匹配 我试图在我的android项目中使用这个库。问题是该库的重量太大(已安装的应用程序大小为41MB,而使用Guava库则变为45MB) 从番石榴图书馆,我只需要这个: CharMatcher.ascii().matchesAllOf(); 你知道我应该如何正确地检查我的字符串吗,或者有其他的轻量级库吗 谢谢 java代码是: public static boolean isAsciiPrintable(String str) { if
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;
}
}
public静态布尔值isAsciiPrintable(String str){
如果(str==null){
返回false;
}
int sz=str.length();
对于(int i=0;i=32&&ch<127;
}
}
参考:您可以尝试以下方法:
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;
}
private静态布尔值isAllASCII(字符串输入){
布尔值isASCII=true;
对于(int i=0;i0x7F){
isASCII=假;
打破
}
}
返回isASCII;
}
参考从的到
你可以用它来做
查看Guava源代码并将该方法和其他调用堆栈复制到本地@Nambari根据您的回答,我不会对许可证有任何问题?@Diyarbakir请在标记之前阅读问题。它是开源的,AFAIK没有许可证问题(但我无权确认,因为我不是该项目的参与者)。您应该继续使用Guava,但缩小您的jar文件!杰出的谢谢你的回答!
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
*/
}
}