Check if letters that a word consists of are present in another string

后端 未结 3 882
后悔当初
后悔当初 2021-01-29 07:35

This is my first time posting here, so please tell me if I need to correct anything in this post. I\'d like to ask for help with something that\'s been giving me some trouble.

3条回答
  •  萌比男神i
    2021-01-29 08:07

    Your program as it is is returning true if any character in your word variable is contained in your randomString variable. Judging from your comments it sounds like you want to check if every character from your word string is contained within your randomString variable. Here is a slightly different approach.

    public static boolean Checkword(){
        String randomString = "BFEABLDEG";
        String word = "LABEL";
        for(int i=0;i

    This is quite inefficient since it has to create a new string each time, if you want to make it a little better, use a string builder to mutate the string to remove the characters as they are found.

提交回复
热议问题