【问题标题】:Comparing two strings in java character by character逐个字符比较java中的两个字符串
【发布时间】:2012-08-02 22:58:51
【问题描述】:

我是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++;    
            }
      }`

有什么建议吗?

【问题讨论】:

  • 计数器是i,但你永远不会在循环中使用它,而是有一些j。为什么?
  • 是什么让你认为 substring(j, j) 会返回任何东西?
  • 您的代码“不起作用”怎么办?编译时会发生什么?如果它编译,它会运行吗?如果它运行,会发生什么?在此过程中,发生了哪些与您的预期不同的事情?另外,如果有的话,您会收到哪些错误消息?
  • 对不起,我的代码中是 j 而不是 i,我使用 char 方法解决了这个问题。非常感谢
  • (但是,您知道为什么您的解决方案不起作用吗?)

标签: java string char compareto


【解决方案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++;    
        }
}

【讨论】:

  • +1 但我会在数组长度上添加一个测试,并且只迭代到最短的末尾。
【解决方案2】:

我在 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) );

}

}

【讨论】:

    【解决方案3】:

    我们可以通过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++;    
      }
    }
    

    【讨论】:

      【解决方案4】:
      int i =0;
      for(char c : w1.toCharArray())){
         if(i < w2.length() && w2.charAt(i++) != c)
           counter++;
      }
      

      【讨论】:

        【解决方案5】:

        使用 charAt(index) 方法并对两个字符使用 '==' 运算符:

        c1 = w1.charAt(j);
        c2 = w2.charAt(j);
        
        if (c1 == c2) ){
           counter++;    
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-14
          • 1970-01-01
          • 1970-01-01
          • 2015-06-17
          • 1970-01-01
          • 2019-10-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多