androd开发: 我有一个String字符串里边可能包含多种语言的文字例如英语、中文、俄语、泰语、阿拉伯语等非常多语言我想要一个接口传入这个字符串进行判断如果字符串只包含阿拉伯语、英文、阿拉伯数字、空格、常用的标点符号 则返回true有示例代码吗
以下是一个示例代码,用于判断字符串是否只包含阿拉伯语、英文、阿拉伯数字、空格和常用的标点符号:
public class LanguageChecker {
public static boolean isSupportedLanguage(String text) {
// 使用正则表达式匹配字符串是否只包含阿拉伯语、英文、阿拉伯数字、空格和常用的标点符号
String regex = "^[a-zA-Z0-9\\s\\p{Punct}]+$";
return text.matches(regex);
}
public static void main(String[] args) {
String text1 = "Hello, World! 123";
String text2 = "مرحبا بكم";
String text3 = "你好,世界!";
String text4 = "สวัสดีชาวโลก";
String text5 = "Hello, 世界!123";
System.out.println(isSupportedLanguage(text1)); // true
System.out.println(isSupportedLanguage(text2)); // true
System.out.println(isSupportedLanguage(text3)); // false
System.out.println(isSupportedLanguage(text4)); // false
System.out.println(isSupportedLanguage(text5)); // false
}
}
在上面的示例代码中,isSupportedLanguage方法使用正则表达式来匹配字符串是否只包含阿拉伯语、英文、阿拉伯数字、空格和常用的标点符号。如果匹配成功,则返回true,否则返回false。在main方法中,我们对几个不同的字符串进行测试,以验证该方法的正确性
原文地址: https://www.cveoy.top/t/topic/ic61 著作权归作者所有。请勿转载和采集!