javascript中的字符串比较 [英] string comparision in javascript

查看:73
本文介绍了javascript中的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须比较两个字符串。如果他们的情况不同,也会发出错误警报。为此,我的代码如下:

  var  a,b; 

a = document .f1.t1.value;
b = document .f1.t2.value;
bb = ' ';
cc = ' ';

for (i = 0 ; i< a.length; i ++)
{
for (j = 0 ; j< b.length; j ++ )
{
if (b.charAt(j)> = 65 && b.charAt(j)< = 90
{
bb = bb + b.charAt(j);
}
else
bb = bb + b.charAt(j);
}

如果(a.charAt(i)> = 65 && a.charAt(i)< = 90
{
cc = cc + a.charAt(i );
}
else
cc = cc + a.charAt(i);
}

如果(bb == cc)
{
alert( 字符串相等);
}
else
alert( 字符串不相等);
< / script>
< / head >
< body>
< form name = f1>
< input type = text name = t1>
< input type = text name = t2>
< input type = button value = ok onclick = REV()>
< / 表格 >
< / body >
< / html >



但是没有执行。我错了吗?

解决方案

< pre lang =Javascript>< script type = text / javascript>
var username = Agent006;
if (用户名== Agent007
document .write( 欢迎特工007);
else
document .write( 拒绝访问!);
document .write( < br />< br />您想再试一次吗?< br />< br />);

// 用户输入其他名称
username = < span class =code-string>
Agent007;
if (用户名== Agent007
document .write( 欢迎特工007);
else
document .write( 拒绝访问!);
< / script>





希望这有帮助。




比较两个字符串:

  function  validate()
{
a = document .f1.t1.value;
b = document .f1.t2.value;
if (a.Length == b.Length)
{
for (i = 0 ; i< a.length; i ++)>
{
if (a.charcodeat(i)!= b.charcodeat(i))
alert(' 出了点问题');
return false ;

}
}
else
{
alert(' 出了点问题');
return false ;
}

}


你的代码毫无意义。

引用:

if(b.charAt(j)> = 65& b.charAt(j)< = 90)

{

bb = bb + b.charAt(j);

}

else

bb = bb + b.charAt(j);

如果条件是 true ,那么你最终会做同样的事情(什么?!),如果它是 false





引用:

if(bb == cc)

{

alert(字符串相等);

}

这里说明基于单个字符的字符串相等。


i have to compare two strings. if their cases are dissimilar than also it alerts a error. for this my code is as follows:

var a,b;

a = document.f1.t1.value;
b = document.f1.t2.value;
bb = '';
cc = '';

for(i=0;i<a.length;i++)
{
    for(j=0;j<b.length;j++)
    {
        if(b.charAt(j) >=65 && b.charAt(j) <= 90)
        {
            bb = bb + b.charAt(j);
        }
        else
            bb = bb +b.charAt(j);
    }

    if (a.charAt(i) > =65 && a.charAt(i) <= 90)
    {
        cc = cc + a.charAt(i);
    }
    else
        cc = cc + a.charAt(i);
    }

    if (bb == cc)
    {
        alert("strings are equal");
    }
    else
        alert("strings are unequal");
</script>
</head>
<body>
    <form name = "f1">
        <input type ="text" name ="t1">
        <input type ="text" name ="t2">
        <input type="button" value="ok" onclick="rev()">
    </form>
</body>
</html>


but is is not executing.where i am wrong?

解决方案

<script type="text/javascript">
    var username = "Agent006";
    if(username == "Agent007")
    	document.write("Welcome special agent 007"); 
    else
    	document.write("Access Denied!"); 
    document.write("<br /><br />Would you like to try again?<br /><br />");
    
    // User enters a different name
    username = "Agent007";
    if(username == "Agent007")
    	document.write("Welcome special agent 007"); 
    else
    	document.write("Access Denied!"); 
</script>



Hope this helps.


Hi,
For Comparing two strings :

function validate()
{
       a = document.f1.t1.value;
       b = document.f1.t2.value;
if(a.Length==b.Length)
{
       for(i=0;i<a.length;i++)>
        {
               if(a.charcodeat(i)!=b.charcodeat(i))
               alert('Something went wrong ');
               return false;
                
        }
}
else
{
  alert('Something went wrong');
  return false;
}

}


Your code makes no sense.

Quote:

if(b.charAt(j) >=65 && b.charAt(j) <= 90)
{
bb = bb + b.charAt(j);
}
else
bb = bb +b.charAt(j);

Here you end up doing the same thing (what?!) either if the condition is true and if it is false.


Quote:

if (bb == cc)
{
alert("strings are equal");
}

Here you are stating the strings are equal based on a single character equality.


这篇关于javascript中的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆