如何检查字符串中的字母/字符是否都在char数组中找到

时间:2018-10-24 01:40:40

标签: java arrays string char

我如何检查字符串是否仅包含某个字符数组中的字母

import java.util.*;

public class Main {

    static char[] f = {'q','w','e','r','t','y','u','i','o','p'};
    static char[] s = {'a','s','d','f','g','h','j','k','l'};
    static char[] t = {'z','x','c','v','b','n','m'};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        for(int i = 0; i < n; i++) {
            String str = sc.nextLine();
            if(string only contains letters from array f) count++;
        }
    }
}

7 个答案:

答案 0 :(得分:2)

这是我的代码,也许可以为您提供帮助:

public static boolean checkIfStringContainsLettersFromArray(String str, char [] arr) {
    String arrString = new String(arr);

    for(int i = 0; i < str.length(); i++) {
        if(arrString.indexOf(str.charAt(i)) < 0) return false; 
    }
    return true;
}

答案 1 :(得分:1)

更改代码以使用正则表达式将是最简单的:

public class Main {
    private static final String f = "[qwertyuiop]*";
    private static final String s = "[asdfghjkl]*";
    private static final String t = "[zxcvbnm]*";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
            if (str.matches(f)) {
                count++;
            }
        }
    }
}

答案 2 :(得分:1)

您可以使用containsAll中的List

char arr[] = "scarywombat".toCharArray();
List<Character> limit = new ArrayList<Character>();
List<Character> input = new ArrayList<Character>();

for (char c : arr) {
    limit.add (c);
    input.add (c);
}

System.out.println(limit);
System.out.println(input);
System.out.println(limit.containsAll(input));

//add extra char
input.add('Z');
System.out.println(limit);
System.out.println(input);
System.out.println(limit.containsAll(input));

输出

[s, c, a, r, y, w, o, m, b, a, t]
[s, c, a, r, y, w, o, m, b, a, t]
true
[s, c, a, r, y, w, o, m, b, a, t]
[s, c, a, r, y, w, o, m, b, a, t, Z]
false

答案 3 :(得分:1)

一种方法是将char数组转换为set并测试字符串中的任何char是否包含不在数组中的任何字符:

import com.google.common.primitives.Chars;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    static char[] f = {'q','w','e','r','t','y','u','i','o','p'};
    static char[] s = {'a','s','d','f','g','h','j','k','l'};
    static char[] t = {'z','x','c','v','b','n','m'};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;

        for(int i = 0; i < n; i++) {
            String str = sc.nextLine();
            if(containsOnly(str, f)) count++;
        }
    }

    private static boolean containsOnly(String stringToSearch, char[] theOnlyChars) {

        Set<Character> collect = new HashSet<>(Chars.asList(theOnlyChars));

        for (int i = 0; i < stringToSearch.length(); i++) {
            char c = stringToSearch.charAt(i);
            if (!collect.contains(c)) {
                return false;
            }
        }

        return true;
    }
}

答案 4 :(得分:1)

static Pattern f = toPattern(new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'});
static Pattern s = toPattern(new char[]{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'});
static Pattern t = toPattern(new char[]{'z', 'x', 'c', 'v', 'b', 'n', 'm','\n'});

private static Pattern toPattern(char[] arr) {
    return Pattern.compile(String.format("[%s]*", new String(arr)), Pattern.MULTILINE);
}

private static boolean checkIfStringContainsLettersFromArray(String str, Pattern whitelist) {
    return whitelist.matcher(str).matches();
}

public static void main(String[] args) {
    System.out.println(checkIfStringContainsLettersFromArray("zxzxzxzxcvbnmxzxz", t)); //true
    System.out.println(checkIfStringContainsLettersFromArray("zxzxzxzxa ", t)); //false
}

答案 5 :(得分:0)

使用java Arrays包,只需检查字符是否在f char数组中即可。如果f char数组中的字符串中至少有1个字符不可用,则程序将停止处理当前字符串,然后继续处理下一个字符串。

import java.util.*;
import java.util.Arrays;

public class Main {
static char[] f = {'q','w','e','r','t','y','u','i','o','p'};
static char[] s = {'a','s','d','f','g','h','j','k','l'};
static char[] t = {'z','x','c','v','b','n','m'};

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int count = 0;
    for(int i = 0; i < n; i++) {
        String str = sc.nextLine();
        char[] chars = str.toCharArray();
        int j = 0;
        for(; j < chars.length; j++){
           if(!Arrays.asList(f).contains(chars[j])){
               break;
           }
        }
        if(j == chars.length){
            count++;
        }
    }
}
}

答案 6 :(得分:-1)

如果是这种情况,请对照char数组的每个字符检查String的每个字符,但这将花费2遍。 如果要提高效率,请将所有元素放入Set中的char数组中,并以固定时间检查其中的char,因此只需要传递一次String,如下所示:

public class Main {

    static char[] f = {'q','w','e','r','t','y','u','i','o','p'};
    static char[] s = {'a','s','d','f','g','h','j','k','l'};
    static char[] t = {'z','x','c','v','b','n','m'};
    //showing one example
    HashSet<Character> fs = new HashSet();
    fs.addAll(f);
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        for(int i = 0; i < n; i++) {
            String str = sc.nextLine();
            int flag = 0;
            for (int l =0;l<str.length();l++){
                if(fs.contains(str.charAt(l)) == false){ flag = 1; break;   }
            }
            if (flag == 0){count++}
            //if(string only contains letters from array f) count++;
        }
    }
}