androd开发: 我有一个String字符串里边可能包含多种语言的文字例如英语、中文、俄语、泰语、阿拉伯语等非常多语言我想要一个接口传入这个字符串进行判断如果字符串只包含捷克语、英文、阿拉伯数字、空格、常用的标点符号 则返回true有示例代码吗
以下是一个示例代码,用于判断一个字符串是否只包含捷克语、英文、阿拉伯数字、空格和常用的标点符号:
public interface LanguageValidator {
boolean validate(String input);
}
public class LanguageValidatorImpl implements LanguageValidator {
@Override
public boolean validate(String input) {
// 定义只包含的语言字符和标点符号
String allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,;:?!";
// 遍历字符串的每个字符
for (char c : input.toCharArray()) {
// 如果字符不在允许的字符集合中,返回false
if (allowedCharacters.indexOf(c) == -1) {
return false;
}
}
return true;
}
}
public class Main {
public static void main(String[] args) {
String input = "This is a sample string.";
LanguageValidator validator = new LanguageValidatorImpl();
boolean isValid = validator.validate(input);
System.out.println("Is valid? " + isValid);
}
}
在上面的示例代码中,LanguageValidator接口定义了validate方法用于判断字符串是否只包含指定的语言字符和标点符号。LanguageValidatorImpl类实现了该接口,并在validate方法中遍历输入字符串的每个字符,检查其是否在允许的字符集合中。如果有任何一个字符不在允许的字符集合中,返回false,否则返回true。
在Main类中,我们创建了一个示例输入字符串,并使用LanguageValidatorImpl实例进行验证。最后,打印出验证的结果
原文地址: https://www.cveoy.top/t/topic/ic7W 著作权归作者所有。请勿转载和采集!