如何检查一个字符串是否以某些给定的字符/模式开头

如何检查一个字符串是否以某些给定的字符/模式开头

在本文中,我们将检查一个字符串是否以某些给定的字符/模式开头。我们可以使用JavaScript中的各种方法来检查给定的字符串是否以指定字符串的字符开头。

检查一个字符串是否以某些给定模式开头的方法:

  • 使用JavaScript循环
  • 使用substring()和localeCompare()方法
  • 使用startsWith()方法
  • 使用JavaScript的indexOf()方法匹配

方法1:使用JavaScript循环

这是一种简单的方法,我们将逐个从开始位置匹配字符,使用循环,如果任何字符不匹配,则可以说字符串不以该字符或指定字符串开头。

语法:

for (let i = 0; i < pattern.length; i++) {  
        if(str.charAt(i) != pattern.charAt(i)){  
            result = false;  
            break;  
        }  
      }  
JavaScript

示例: 下面的程序演示了上述方法:

// Javascript script to check
// whether the String begins
// with something or not
 
// Function to check String
// begins with something or not
function check (str, pattern) {
  // Initially we assume that String
  // begins with something
  // so result is true
  let result = true
 
  // Loop to match character by character
  for (let i = 0; i < pattern.length; i++) {
    // If any character doesn't matches
    // then result is false
    if (str.charAt(i) != pattern.charAt(i)) {
      result = false
      break
    }
  }
 
  if (result) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'GeeksforGeeks'
 
// Pattern by which string
// begins or not
let pattern = 'Geeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'geeksforgeeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)
JavaScript

输出

String = "GeeksforGeeks"
String should begin with = "Geeks"
String begins with "Geeks"
String = "geeksforgeeks"
String should begin with = "Geeks"
String doesn't begins with "Geeks"
JavaScript

方法2:使用substring()和localeCompare()方法

在这种方法中,我们将使用substring()函数获取所需长度的模式字符串的子字符串,然后使用localeCompare()函数将子字符串与模式进行匹配。

语法:

let substr = string.substring(Startindex, Endindex)  
string.localeCompare(substr)  
JavaScript

示例: 下面的程序演示了上面的方法:

// Javascript script to check
// whether the String begins
// with something or not
 
// Function to check String
// begins with something or not
function check (str, pattern) {
  // Extracting substring of
  // pattern length
  let sub_str = str.substring(0, pattern.length)
 
  if (sub_str.localeCompare(pattern) == 0) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'GeeksforGeeks'
 
// Pattern by which string
// begins or not
let pattern = 'Geeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'geeksforgeeks'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)
JavaScript

输出

String = "GeeksforGeeks"
String should begin with = "Geeks"
String begins with "Geeks"
String = "geeksforgeeks"
String should begin with = "Geeks"
String doesn't begins with "Geeks"
JavaScript

方法3:使用字符串的startsWith()方法

语法:

在这个方法中,我们将使用startsWith()方法直接检查给定字符串是否以某个字符串开头。

str.startsWith( searchString , position )  
JavaScript

示例: 以下程序演示了上述方法:

// Javascript script to check
// whether the String begins
// with something or not
 
// Function to check String
// begins with something or not
function check (str, pattern) {
  if (str.startsWith(pattern)) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'Burn to shine'
 
// Pattern by which string
// begins or not
let pattern = 'Burn'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'Happy coding'
// Change pattern
pattern = 'happy'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)
JavaScript

输出

String = "Burn to shine"
String should begin with = "Burn"
String begins with "Burn"
String = "Happy coding"
String should begin with = "happy"
String doesn't begins with "happy"
JavaScript

方法4:使用JavaScript的indexOf()方法

示例: 在这个示例中,我们将使用 JavaScript的indexOf()方法 来检查模式在索引0位置是否存在,如果存在则返回true,否则返回false。

// Javascript script to check
// whether the String begins
// with something or not
 
// Function to check String
// begins with something or not
function check (str, pattern) {
  if (str.indexOf(pattern) === 0) {
    console.log('String begins with "' + pattern + '"')
  } else {
    console.log("String doesn't " + 'begins with "' + pattern + '"')
  }
}
 
// Driver code
// String to check
let str = 'Burn to shine'
 
// Pattern by which string
// begins or not
let pattern = 'Burn'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Call check function
check(str, pattern)
 
// Change string
str = 'Happy coding'
// Change pattern
pattern = 'happy'
 
console.log('String = "' + str + '"')
console.log('String should begin with = "' + pattern + '"')
 
// Calling check function
check(str, pattern)
JavaScript

输出

String = "Burn to shine"
String should begin with = "Burn"
String begins with "Burn"
String = "Happy coding"
String should begin with = "happy"
String doesn't begins with "happy"
JavaScript

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册