How can I iterate over a string in Java?

前端 未结 9 1777
余生分开走
余生分开走 2021-01-02 12:40
public static Boolean cmprStr( String s1, String s2 )
{
    // STUFF
}

I want to iterate through s1 to make sure that every character in s1 is incl

9条回答
  •  情书的邮戳
    2021-01-02 13:12

    As I understand the question it would be.

    //for each character in s1
      //if s2 does not contain character return false
    
    //return true
    
    for(int i = 0; i < length s1; i++){
      if(!s2.contains(String.valueOf(s1.charAt(i)))){
        return false;
      }
    }
    return true;
    

    This verifies that each character in s1 is in s2. It does not confirm order, nor how many are there, and is not an equals method.

    Recursive:

    public static Boolean cmprStr( String s1, String s2 )
    {
      if(s1.length() == 0 )
      {
        return true; 
      }
      if(!s2.contains(s1.substring(0,1)))
      {
        return false;
      }
      return cmprStr(s1.substring(1), s2);
    }
    

提交回复
热议问题