简体   繁体   English

如何检查字符串是否包含法语字母?

[英]How to check if a string contains French letters?

在Java中,如何检查字符串是否包含法语字母?

public static boolean containsFrench(String s) {
    Pattern frenchPattern = Pattern.compile("(?i)[çèéêîôœû]");
    return frenchPattern.matcher(s).find();
}

Since the notion of a "french letter" is poorly defined, the simplest way to solve this would be to create an array containing all letters that you think qualify, and then just test each character in the string to see if it is in the array: 由于“法语字母”的概念定义不明确,解决此问题的最简单方法是创建一个包含您认为有资格的所有字母的数组,然后仅测试字符串中的每个字符以查看其是否在数组中:

(I'm not going to write the code for you because this is something that anyone who has done a Java course or tutorial should be able to write in five minutes. And if you can't do it in 5 minutes, then you need to practice by doing it yourself.) (我不会为您编写代码,因为完成Java课程或教程的任何人都应该可以在5分钟内完成编写。如果您在5分钟内无法完成编写,则需要自己动手练习 。)

There are more elegant and/or more efficient ways to do this. 有更优雅和/或更有效的方法可以做到这一点。 But simple is good for a beginner. 但是简单对初学者是有好处的。

Quick solution - you might want to check that the list is exhaustive (for example, I have not included accented large caps or the ÿ which is not very common) but the concept should work: 快速解决方案-您可能要检查列表是否详尽(例如,我未包括带重音符号的大写字母或不太常见的ÿ),但该概念应该有效:

public static void main(String[] args) {
    Set<Character> frenchLetters = new HashSet<> (Arrays.asList('â', 'à', 'ç', 'é', 'ê', 'ë', 'è', 'ï', 'î', 'ô', 'û', 'ù'));
    String s = "abcdà";
    for (char c : s.toCharArray()) {
        if (frenchLetters.contains(c)) {
            System.out.println("Found a French letter: " + c);
        }
    }
}

Try: 尝试:

public boolean hasFrenchCharacter(String input) {
    if (input.contains("ù") == true) return true;
    if (input.contains("û") == true) return true;
    if (input.contains("ü") == true) return true;
    if (input.contains("ÿ") == true) return true;
    if (input.contains("à") == true) return true;
    if (input.contains("â") == true) return true;
    if (input.contains("æ") == true) return true;
    if (input.contains("ç") == true) return true;
    if (input.contains("é") == true) return true;
    if (input.contains("è") == true) return true;
    if (input.contains("ê") == true) return true;
    if (input.contains("ë") == true) return true;
    if (input.contains("ï") == true) return true;
    if (input.contains("î") == true) return true;
    if (input.contains("ô") == true) return true;
    if (input.contains("œ") == true) return true;
    return false;
}

It will return true if it has a French character, false otherwise. 如果具有法语字符,它将返回true,否则返回false。

Really this is not the way I would do it, I would do it this way: 确实这不是我要这样做的方式,我会这样做:

public boolean hasFrenchCharacter(String input) {
    return input.contains("ù") ||
           input.contains("û") ||
           input.contains("ü");
    //and so on...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM