How can I iterate over a string in Java?

前端 未结 9 1676
余生分开走
余生分开走 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:33
    public static Boolean cmprStr( String s1, String s2 )
    {
        for (int i = s1.length() - 1; i >= 0; --i) {
             if (s2.indexOf(s1.charAt(i)) == -1) {
                 return Boolean.FALSE;
             }
        }
        return Boolean.TRUE;
    }
    
    0 讨论(0)
  • Every String is also a CharSequence in Java. Therefore, you can easily iterate over a String using a simple for loop:

    int n = s.length();
    for (int i = 0; i < n; ++i) {
        char c = s.charAt(i);
        ...
    }
    
    0 讨论(0)
  • 2021-01-02 13:36

    Why don't you simply use 'equals' method ?

    Boolean b = s1.equals(s2);
    
    0 讨论(0)
提交回复
热议问题