如何正确检查瓦贾文两圣迹之间的平等?
I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...
如何正确检查瓦贾文两圣迹之间的平等?
<><><><>always 在你完全理解使用<代码>=和=
的操作者之前,使用<代码>=的操作者,因为这将使你免于模糊不清(不相干) bugs和WTFs。 “常规”<代码>=的操作者,由于内部的类型编码,可以产生非常出乎意料的结果,因此使用<代码>=始终是所建议的做法。
了解这一点,以及Javascript中由Douglas Crockford先生及其工作阅读的其他“好诉坏”部分。 一位伟大的谷歌技术会谈,他在那里总结了许多良好信息:。
<>Update:
The You Don tnow JS series by Kyle Thompson isillian (and free to Read online). 这些丛书涉及语言中常见的误解领域,并解释了“坏部分”是卡罗福德建议你避免的。 通过理解,你可以适当利用这些工具,避免陷阱。
“Up & Going.”书包括一个章节:
在各种情况下,把全部细节整理到少数简单出走者身上,并帮助你知道是否使用 我仍然建议Crockford为不希望投入时间真正理解Javascript的开发商进行会谈,为偶尔在Javascript工作的开发商提供良好建议。
=
或=
。
true
or false
value, avoid ==
and use ===
.0
, ""
, or []
-- empty array), avoid ==
and use ===
.==
. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.
如果你知道他们正在扼杀,那么就没有必要检查。
"a" == "b"
然而,注意到扼杀物并不相同。
new String("a") == new String("a")
否则就将不实。
要求将价值Of()方法转换成对强硬物体的专利,
new String("a").valueOf() == new String("a").valueOf()
遣返
仅加上答复: 如果所有这些方法都回落不实,即使扼杀似乎相等,就有可能给左边和右边留下一个白色空间。 因此,在缩略语结尾处仅添加一个<代码>>。
if(s1.trim() === s2.trim())
{
// your code
}
I have lost hours trying to figure out what is wrong. Hope this will help to someone!
确实有两种方式可以用 j印进行扼杀。
<代码>var str = Javascript ; 这产生了原始的扼杀价值。
var obj = new String( Javascript );
This creates a wrapper object
of type String
.
typeof str // string
typeof obj // object
因此,检查平等的最佳途径是使用=
的操作者,因为它检查了两种操作方式的价值和类型。
如果你想要检查两个物体之间的平等,那么使用<编码>String.prototype. ValueOf是正确的。
new String( javascript ).valueOf() == new String( javascript ).valueOf()
标题 可使用<代码>JSON.stringification(<>/code> trick进行核对。
var me = new String("me");
var you = new String("me");
var isEquel = JSON.stringify(me) === JSON.stringify(you);
console.log(isEquel);
为了进行简单的比较,使用=
,以检查严格的平等。 正如其他人指出的,这样做的好处是,效率最高,减少了胆识或不确定代码的可能性。 资料来源:MDN Web Docs:Strict Equality。
var a = "hello1";
var b = "hello2";
console.log("a === a?" + (a === a) + "|");
console.log("a === b?" + (a === b) + "|");
如果你要比较两条插图,以了解在另一条插图之前或之后,根据自然分类,使用<代码><;>,><=和;=
的操作者。 资料来源:
var a = "hello1";
var b = "hello2";
console.log("a < a?" + (a < a) + "|");
console.log("a < b?" + (a < b) + "|");
console.log("a > b?" + (a > b) + "|");
console.log("b > a?" + (b > a) + "|");
考虑到这两种星体可能非常大,有两种主要方法:<条码>查询<>/条码>和<条码>。
我重新启动了这一职能。
function compareLargeStrings(a,b){
if (a.length !== b.length) {
return false;
}
return a.localeCompare(b) === 0;
}
在地体方面,我们有一个辅助方法 localCompare
,该方法在地貌比较方面非常精干。 海事组织,我们只应当使用它,而且没有必要使问题复杂化。
使用:
const a = Hello
const b = Hell
a.localCompare(a) // 0
a.localCompare(b) // 1
b.localCompare(a) // -1
I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...
The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I tried to print all the possible combination of members of several vectors. Why the function below doesn t return the string as I expected? #include <iostream> #include <vector> #...
I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...
I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??
Is there a PHP string function that transforms a multi-line string into a single-line string? I m getting some data back from an API that contains multiple lines. For example: <p>Some Data</...
I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...