用于检查“|”的javascript函数符号是否存在于字符串中 [英] javascript function to check "|" symbol is present in a string or not

查看:69
本文介绍了用于检查“|”的javascript函数符号是否存在于字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串信息包含以下字符串:

var strMessage =Plcc-failed |分数通过|破产通过|业务年限

即包含一个 | 符号或者这个

var strmsg =失败。

我需要一个javascript函数来检查它是否包含一个失败msg或| 符号。

如何为它编写javascript函数。



提前致谢

My string messege contain the string either this:
var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business
i.e. it contains a " | " symbol in it or this
var strmsg="fail".
I need a javascript function to check whether it contains a "fail" msg or " | " symbol.
How to write a javascript function for it.

Thanks in advance

推荐答案

你可以在字符串上使用indexOf方法。



You can use the indexOf method on the string.

var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business"
var intIndexLocation = strMessage.indexOf("fail");
if (intIndexLocation >= 0)
{
  alert("fail is in the string");
}


你必须对RyanDev的解决方案进行小的改动,在这两种情况下都会出现失败,如果条件对两个字符串都返回true,那么你必须找到的索引相反。



You have to do small change to RyanDev's solution, "fail" is there in both the case and if condition will return true for both strings so you have to find the index of | instead.

var strMessage = "Plcc-failed|Score-passed|bankruptcy-passed|Years in Business";
var intIndexLocation = strMessage.indexOf("|");
if (intIndexLocation >= 0)
{
  alert("| is in the string");
}


这篇关于用于检查“|”的javascript函数符号是否存在于字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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