Results 1 to 4 of 4
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default Coding question to check if string contains some chars?

    Hello I have below program in this program I want to check if string a contains all char in char[] b array, with the example of String a="cash" and char[] b={'c', 'h'}, I have correct result, unfortunately my string and char array will be dynamic, will change during the program and if I have String a="h" and char[] b={'c', 'h'}, my program also gives message "Yes it contains", but this is not true accoding to what I try to accomplish, I want to get "No it does not contain message". Can you guys help me?
    Java Code:
    package stringcheck;
    
    public class StringCheck 
    {
        String a="cash";
        char[] b={'c', 'h'};
        
        public StringCheck()
        {
            
        }
        public static void main(String[] args)
        {
            StringCheck mainClass = new StringCheck();
            mainClass.execute();
        }
        
        public void execute()
        {
            if(containsAny(a,b))
            {
                System.out.print("Yes it contains");
            }
            else
            {
                System.out.print("No it  does not contain");
             }
        }
        public static boolean containsAny(String str, char[] searchChars) {
          if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) {
              return false;
          }
          for (int i = 0; i < str.length(); i++) {
              char ch = str.charAt(i);
              for (int j = 0; j < searchChars.length; j++) {
                  if (searchChars[j] == ch) {
                      return true;
                  }
              }
          }
          return false;
      }
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Coding question to check if string contains some chars?

    What are the conditions for a correct match?

  3. #3
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default Re: Coding question to check if string contains some chars?

    condition is if (string value contains all character in the char array )
    Let me give you more example:
    Example A)
    String a ="abcdef"
    char[] b = {'b', 'e', 'a'}
    result should be "Yes it contains"

    Example B)
    String a="abc"
    char[] b = {'b', 'd'}
    result should be "No it does not contains"

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Coding question to check if string contains some chars?

    String method indexOf would be my choice, it searches through a String for a char. If it finds the char it returns the index position if not -1.
    Use a for loop to loop through the char array calling the strings indexOf method on each char in the array. If it returns -1 you know it doesn't match.

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2011, 03:46 AM
  2. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  3. Make String into chars
    By myst in forum New To Java
    Replies: 19
    Last Post: 06-20-2010, 04:24 PM
  4. Swap chars in a String?
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 06-08-2010, 09:05 PM
  5. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •