📌  相关文章
📜  如何在 jQuery 中检查字符串以特定字符串开头/结尾?

📅  最后修改于: 2021-11-07 08:17:37             🧑  作者: Mango

JavaScript 提供了很多字符串方法来检查一个字符串是否是其他字符串的子字符串。因此,根本不需要 jQuery 来执行此任务。但是,我们将覆盖检查是否字符串开始或结束与一个字符串的所有不同的方式:

  • startsWith() 和 endsWith() 方法
  • 搜索()方法
  • indexOf() 方法
  • substring() 方法
  • substr() 方法
  • 切片()方法

让我们考虑一个字符串str = “Welcome to GEEKS FOR GEEKS!!!”。现在我们必须找出字符串str 是否以 startword = “Welcome” 开头并以 endword = “!!!” 结尾。

方法:
startsWith()endsWith()方法:它检查字符串是否以特定子字符串开头或结尾。

例子:

Javascript
if (str.startsWith(startword) && str.endsWith(endword)) {
 
    // case sensitive
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript
if (str.search(startword)==0 &&
    str.search(endword)==stringlength-endwordlength ) {
 
    // case sensitive
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript
if (str.indexOf(startword)==0 &&
    str.indexOf(endword)==stringlength-endwordlength) {
             
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript
if(str.substring(0, startwordlength)==startword
        && str.substring(stringlength-endwordlength,
        stringlength)==endword) {
         
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript
if(str.substr(0, startwordlength)==startword
        && str.substr(stringlength-endwordlength,
        endwordlength)==endword) {
 
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript
if(str.slice(0, startwordlength)==startword
        && str.slice(stringlength-endwordlength,
        stringlength)==endword) {
 
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}


Javascript


 

    
        How to know that a string starts/ends
        with a specific string in jQuery?
    
 
    

 

    

        Welcome To GEEKS FOR GEEKS!!!     

      

        Check whether a word is present         in the above sentence     

                       

startsWith() and endsWith() method

    

      

indexOf() method

    

      

search() method

           

substring() method

    

      

substr() method

    

      

slice() method

    

          


search() 方法:它检查特定字符串是否包含在另一个字符串,并返回该子字符串的起始索引。

例子:

Javascript

if (str.search(startword)==0 &&
    str.search(endword)==stringlength-endwordlength ) {
 
    // case sensitive
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}

的indexOf()方法:作为顾名思义,它找到的字符串中的子串的起始索引。

例子:

Javascript

if (str.indexOf(startword)==0 &&
    str.indexOf(endword)==stringlength-endwordlength) {
             
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}

substring() 方法:它返回存在于开始和结束索引之间的字符串。

例子:

Javascript

if(str.substring(0, startwordlength)==startword
        && str.substring(stringlength-endwordlength,
        stringlength)==endword) {
         
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}

substr() 方法:与 substring() 方法类似,但以子串的起始索引和长度为参数。

例子:

Javascript

if(str.substr(0, startwordlength)==startword
        && str.substr(stringlength-endwordlength,
        endwordlength)==endword) {
 
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}

slice()方法:该方法返回字符串的任何两个索引之间的切片中的字符串。

例子:

Javascript

if(str.slice(0, startwordlength)==startword
        && str.slice(stringlength-endwordlength,
        stringlength)==endword) {
 
    console.log("Yes, The string " + str + " starts with "
            + startword + " and ends with " + endword);
}

让我们以 h1 元素中的文本为例,检查它是否以给定的子字符串开头和结尾。

Javascript



 

    
        How to know that a string starts/ends
        with a specific string in jQuery?
    
 
    

 

    

        Welcome To GEEKS FOR GEEKS!!!     

      

        Check whether a word is present         in the above sentence     

                       

startsWith() and endsWith() method

    

      

indexOf() method

    

      

search() method

           

substring() method

    

      

substr() method

    

      

slice() method

    

          

输出: