逐字符比较java中的两个字符串
我是java初学者,我试图通过下面的代码逐个比较java中的两个字符串,并找出它们有多少不同的字符,但这不起作用逐字符比较java中的两个字符串,java,string,char,compareto,Java,String,Char,Compareto,我是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
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++;
}
}`
min是两个字符串之间的最小值
对于(int i=0;i使用以下公式:
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++;
}
}
char[]first=w1.toLowerCase().toCharArray();
char[]second=w2.toLowerCase().toCharArray();
int minLength=Math.min(first.length,second.length);
对于(int i=0;i
使用charAt(index)方法并对两个字符使用“==”运算符:
c1 = w1.charAt(j);
c2 = w2.charAt(j);
if (c1 == c2) ){
counter++;
}
inti=0;
for(字符c:w1.toCharArray()){
if(i
我们可以用子字符串解决这个问题。但让我们先看看您的代码:
// 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++;
}
}
我在java培训教程中的笔记要求使用charAt()和嵌套循环进行字符串比较。。。该方法可以很容易地更改为从源字符串返回不匹配的字符。。。但我会让你来决定…;-)
公共类子字符串{
公共静态布尔findTarget(字符串目标,字符串源){
int target_len=target.length();
int source_len=source.length();
布尔值=false;
对于(int i=0;(i=target\u len){
打破
}
/**
*学习理念:
*
*字符串target=“for”;
*String source=“在字符串中艰难地搜索字符串。”;
*
*1-目标特征(j):
*位置0处的字符>目标字符“f”中的第一个字符,索引为0。
*
*2-源字符(i+j):
*
*将搜索源字符串的数组索引,以确定是否找到源字符串的匹配项
*目标字符串的字符索引位置。目标字符串中每个字符的位置
*然后与源字符串中字符的位置进行比较。
*
*如果该条件为true,则目标循环将持续目标字符串的长度。
*
*如果所有源字符串的字符数组元素位置与目标字符串的字符数组元素位置匹配
*那么条件成功了。。
*/
else if(target.charAt(j)!=source.charAt(i+j)){
打破
}否则{
++j;
如果(j==target_len){
发现=真;
}
}
}
}
发现退货;
}
公共静态void main(字符串…参数){
字符串target=“for”;
String source=“在字符串中艰难地搜索字符串。”;
System.out.println(findTarget(target,source));
}
}
+1但我会对数组长度添加一个测试,并且只迭代到最短长度的末尾。计数器是I
,但您从不在循环中使用它,而是使用一些j
。为什么?是什么让你认为子字符串(j,j)会返回任何东西?你的代码“不工作”怎么办?编译它时会发生什么?如果它编译了,它会运行吗?如果它运行,会发生什么?一路上,会发生什么与您的期望不同的事情?另外,如果有的话,您会收到什么错误消息?很抱歉,在我的代码中是j而不是i,我使用char方法解决了这个问题。非常感谢您(但是,您知道您的解决方案为什么不起作用吗?)
// 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++;
}
}
for(int j=0; j < min; j++) {
s1 = w1.substring(j,j+1);
s2 = w2.substring(j,j+1);
if (!s1.equalsIgnoreCase(s2) ){
counter++;
}
}
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) );
}
}