通过字符Java字符两个字符串比较(Comparing two strings in java ch

2019-06-26 18:33发布

我在初学java,我想通过字符在Java焦炭两个字符串比较,发现他们有多少不同的字符具有由下面的代码,但它不工作,

     min is the min between the 2 strings

     for(int i=0; i<min-1; i++){
            s1 = w1.substring(j,j++);
            s2 = w2.substring(j,j++);

            if (! s1.equalsIgnoreCase(s2) ){
                counter++;    
            }
      }`

有小费吗?

Answer 1:

用这个:

char[] first  = w1.toLowerCase().toCharArray();
char[] second = w2.toLowerCase().toCharArray();

int minLength = Math.min(first.length, second.length);

for(int i = 0; i < minLength; i++)
{
        if (first[i] != second[i])
        {
            counter++;    
        }
}


Answer 2:

使用的charAt(index)方法,并使用“==”操作两个字符:

c1 = w1.charAt(j);
c2 = w2.charAt(j);

if (c1 == c2) ){
   counter++;    
}


Answer 3:

int i =0;
for(char c : w1.toCharArray())){
   if(i < w2.length() && w2.charAt(i++) != c)
     counter++;
}


Answer 4:

我们可以解决这个问题substring 。 但是,让我们看看你的代码首先:

// assuming, min is the minimum length of both strings,
// then you don't check the char at the last position
for(int j=0; j < min-1; j++) {

  // s1, s2 will always be empty strings, because j++ is post-increment:
  // it will be incremented *after* it has been evaluated
  s1 = w1.substring(j,j++);
  s2 = w2.substring(j,j++);

  if (!s1.equalsIgnoreCase(s2) ){
    counter++;    
  }
}

基于溶液substring可能是这样的:

for(int j=0; j < min; j++) {
  s1 = w1.substring(j,j+1);
  s2 = w2.substring(j,j+1);

  if (!s1.equalsIgnoreCase(s2) ){
    counter++;    
  }
}


Answer 5:

从java培训教程我的笔记需要用的charAt()和嵌套循环字符串比较...的方法可以很容易地修改,从源字符串返回不匹配的字符...但我会离开,一个由你... ;-)

public class SubString {

public static boolean findTarget( String target, String source ) {

    int target_len = target.length();
    int source_len = source.length();

    boolean found = false;

    for(int i = 0; ( i < source_len && !found ); ++i) {

    int j = 0;

        while( !found ) {

            if( j >= target_len ) {
                break;
            }

            /**
             * Learning Concept:
             *
             *  String target = "for";
             *  String source = "Searching for a string within a string the hard way.";
             *
             *  1 - target.charAt( j ) :
             *    The character at position 0 > The first character in 'Target' > character 'f', index 0.
             *
             *  2 - source.charAt( i + j) :
             *
             *    The source strings' array index is searched to determine if a match is found for the
             *    target strings' character index position. The position for each character in the target string
             *    is then compared to the position of the character in the source string.
             *
             *    If the condition is true, the target loop continues for the length of the target string.
             *
             *    If all of the source strings' character array element position matches the target strings' character array element position
             *    Then the condition succeeds ..
             */

            else if( target.charAt( j ) != source.charAt( i + j ) ) {
                break;
            } else {
                ++j;
                if( j == target_len ) {
                    found = true;
                }
            }
        }

    }

    return found;

}

public static void main ( String ... args ) {

String target = "for";
String source = "Searching for a string within a string the hard way.";

System.out.println( findTarget(target, source) );

}

}


文章来源: Comparing two strings in java character by character