javascript javascript中的字符串比较返回一个布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10705160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:46:21  来源:igfitidea点击:

string compare in javascript that returns a boolean

javascript

提问by magicianiam

Is there a function in javascript that compares a string and returns a boolean? I found .match but it returns the strings that matched. I was hoping there was something else so that I would have a lesser code in comparing a string. Since I wanted to check if a string has this word and proceed else not.

javascript中是否有比较字符串并返回布尔值的函数?我找到了 .match 但它返回匹配的字符串。我希望还有其他东西,以便我在比较字符串时使用较少的代码。因为我想检查一个字符串是否有这个词,否则没有。

thanks

谢谢

回答by Noah Freitas

You can use the RegEx test()method which returns a boolean:

您可以使用test()返回布尔值的 RegEx方法:

/a/.test('abcd'); // returns true.

回答by Aliostad

You may use type augmentation, especially if you need to use this function often:

您可以使用类型增强,特别是如果您需要经常使用此功能:

String.prototype.isMatch = function(s){
   return this.match(s)!==null 
}

So you can use:

所以你可以使用:

var myBool = "ali".isMatch("Ali");


General view is that use of type augmentation is discouragedonly because of the fact that it can collide with other augmentations.

一般观点认为,不鼓励使用类型增强,因为它可能与其他增强相冲突。

According to Javascript Patternsbook, its use must be limited.

根据Javascript Patternsbook,它的使用必须受到限制。

I personally think it is OK, as long as you use a good naming such as:

我个人认为是可以的,只要你使用一个好的命名比如:

String.prototype.mycompany_isMatch = function(s){
   return this.match(s)!==null 
}

This will make it uglybut safe.

这将使它丑陋安全

回答by Crayon Violent

there is .indexOf()which will return the position of the string found, or -1 if not found

.indexOf()哪个将返回找到的字符串的位置,如果未找到则返回-1

回答by Anderson Pimentel

myString.indexOf(myWord) > -1

or, if you want a function:

或者,如果你想要一个函数:

function hasWord(myString, myWord) {
  return myString.indexOf(myWord) > -1;
}

回答by ArtoAle

Actually, .match()can do the trick for you because it returns an array of pattern matching strings if any, null otherwise.

实际上,.match()可以为您解决问题,因为它返回一个模式匹配字符串数组(如果有),否则返回 null。

Anyway, if you just want to check if a string contain another string you're more likely to use indexOf()(it will return -1 if no substring is found).

无论如何,如果您只想检查一个字符串是否包含另一个您更有可能使用的字符串 indexOf()(如果没有找到子字符串,它将返回 -1)。

The first solution is a bit overkill for your purpose.

第一个解决方案对您的目的来说有点矫枉过正。

If on the other end you want to check for equality you can use `string1 === string2``

如果在另一端你想检查相等性,你可以使用 `string1 === string2`

回答by Jairo Henrique

If anyone still has doubts ...

如果还有人有疑问...

It was quoted by another colleague using the indexOf()method. The return of this method is either "-1", if it does not find the String in the Array, or the position of the case it finds.

另一位同事使用该indexOf()方法引用了它。此方法的返回值要么是“-1”,如果它在 Array 中没有找到 String,要么是它找到的 case 的位置。

To use it with the desired return, you would have to perform a check before, such as:

要将其与所需的回报一起使用,您必须先执行检查,例如:

exampleMethod () {
const expression = ['string1', string2] .indexOf (this.exampleObject);
if (expression! = -1) {
 return true;
  } else return false;
}

Basically, you would take advantage of this return.

基本上,您会利用这种回报。

However, the includes()method can simply be used:

但是,该includes()方法可以简单地使用:

exampleMethode(): boolean {
  return ['string1', 'string2'].includes(this.exampleObject);
}

The return of this method will do what you want in a simple way. Compares the object string with the string array you want to check, and returns a boolean.

此方法的返回将以简单的方式执行您想要的操作。将对象字符串与要检查的字符串数组进行比较,并返回一个布尔值。

回答by Bill

You can just use a compare.

你可以只使用一个比较。

if ("dog" == "cat") {
  DoSomethingIfItIsTrue();
} else {
    DoSomethingIfItIsFalse();
}

回答by Cecil Theodore

I know this isn't the exact answer you are looking for but this is always an effective way to do this.

我知道这不是您正在寻找的确切答案,但这始终是一种有效的方法。

if(var1 == var2){
  //do this
}else{
  //do that
};