简体   繁体   English

带双引号插值的Javascript中的字符串比较

[英]String comparision in Javascript with double quote interpolation

I have a javascript variable var a = "SUCCESS"; 我有一个javascript变量var a =“ SUCCESS”; and while doing some validation I am getting someother value var b = SUCCESS; 在进行验证时,我得到了另一个值var b = SUCCESS; Now I want to compare 现在我要比较

If (a == b){
alert("Allowed");
}else {
alert("Denied");
}   

Above code is not working due to double qoute problem of var b. 由于var b的双重qoute问题,以上代码无法正常工作。

Is there any way to compare ("SUCCESS" == SUCCESS) ? 有什么方法可以比较("SUCCESS" == SUCCESS) I want to assign double quote for variable b. 我想为变量b分配双引号。

If we try this way (a == "b") then value of b will not interpolate. 如果我们尝试这种方式(a == "b")则b的值将不会插值。 Is there any way to use var b inside double quote? 有什么办法在双引号内使用var b吗?

========= =========

I am adding some more Information here 我在这里添加更多信息

First of all Thanks a lot Shinpou !! 首先,非常感谢新浦! because you are very close to my problem !! 因为你非常接近我的问题! var a and var b will not have static values it will be assigned at run time. var a和var b将没有静态值,它将在运行时分配。 var a like "Jan" or "Feb" or "Mar" etc.. var b like Jan or Feb or Mar ...If I compare (a == b ) then it will be false.So I want to add double quote to value of var b like perl qq [""]...If qq[""] is not available in Javascript,Can we convert value of var b to a string object ? var a,例如“ Jan”或“ Feb”或“ Mar”等。var b诸如Jan或Feb或Mar ...如果我比较(a == b)则为false。所以我想添加双引号像perl qq [“”] ...一样将var b的值转换为字符串对象吗? Finally I want value of var b also like "Jan" or "Feb" etc ..So I can compare "if (a== b) " Thanks again !!! 最后,我想要var b的值也像“ Jan”或“ Feb”等..所以我可以比较“ if(a == b)”,再次感谢!

Short answer is no. 简短的答案是没有。 It's really questionable what you are trying to do here. 您要在这里做什么真的很可疑。 a happens to be a variable storing data of type string whose value is "SUCCESS" a恰好是一个变量,用于存储字符串类型的数据,其值为“ SUCCESS”

b is storing SUCCESS. b正在存储成功。 From your question, it is not clear what SUCCESS means here. 根据您的问题,不清楚“成功”在这里意味着什么。 If it is another variable defined somewhere else then it is fine. 如果它是在其他地方定义的另一个变量,那很好。 In that case, if var SUCCESS = "SUCCESS", then a == b will give you true. 在这种情况下,如果var SUCCESS =“ SUCCESS”,则a == b将使您成真。

But if SUCCESS is not defined at all, then your comparison is equivalent to a == undefined 但是,如果根本没有定义SUCCESS,那么您的比较就等于== undefined

As I understand from your question, you have two variables: 从您的问题中我了解到,您有两个变量:

var a = "SUCCESS",
    b = SUCCESS;

If you are declaring the variables as I just did, you are assigning the SUCCESS string to the a variable, while your b variable will equal to the SUCCESS VARIABLE. 如果像我刚才那样声明变量,则将SUCCESS字符串分配给a变量,而b变量将等于SUCCESS VARIABLE。 Basically, if you would want to check for (a == b) and expect a true evaluation, you would have to define the SUCCESS variable and assign it the same value as the a variable: 基本上,如果您想检查(a == b)并期望得到真实的评估,则必须定义SUCCESS变量并将其与a变量分配相同的值:

var SUCCESS = "SUCCESS",
    a = "SUCCESS",
    b = SUCCESS;

a == b // now true

If you do not declare the SUCCESS variable and try to assign it as a value to another variable, you will get a Reference Error when you are comparing a and b, because you didn't declare any SUCCESS variable in your code. 如果您未声明SUCCESS变量并尝试将其作为值分配给另一个变量,则在比较a和b时会出现“ 引用错误” ,因为您没有在代码中声明任何SUCCESS变量。

Also, regarding your (a == "b") comparison, when you write it like this (considering the fact that your a variable holds the "SUCCESS" string, this condition would evaluate the following: 此外,关于您的(a == "b")比较,当您这样编写比较时(考虑到您的变量包含"SUCCESS"字符串的事实,此条件将评估以下内容:

(a == "b") // this is basically "SUCCESS" == "b", which obviously will return false, since the strings are different

It's easy to convert an existing variable to a string, but your first need to make sure that you declared that variable and have assigned a value to it. 将现有变量转换为字符串很容易,但是首先需要确保声明了该变量并为其分配了值 Simply use the toString() function on the variable in order to change its type to string: 只需在变量上使用toString()函数即可将其类型更改为字符串:

var a = 25;

console.log(a); // This will print 25
console.log(typeof a); // This will print Number

a = a.toString(); // This will convert a from 25 to "25"

console.log(a) // Now, this will print "25"
console.log(typeof a); // You will see that a is a String now

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM