Podemos verificar que la string dada comience con los caracteres de la string especificada o no mediante varios métodos en javascript que se describen a continuación:
Método 1: este es un enfoque simple en el que uniremos los caracteres uno por uno desde el inicio usando el ciclo y si algún carácter no coincide, podemos decir que la string no comienza con el carácter o la string especificada.
Sintaxis:
for (var i = 0; i < pattern.length; i++) { if(str.charAt(i) != pattern.charAt(i)){ result = false; break; } }
Ejemplo: El siguiente programa demuestra el enfoque anterior:
<script> // 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 var result = true; // Loop to match character by character for (var 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){ document.write("String begins with \"" + pattern + "\"<br><br>"); } else{ document.write("String doesn't " + "begins with \"" + pattern + "\"<br><br>"); } } // Driver code // String to check var str = "GeeksforGeeks"; // Pattern by which string // begins or not var pattern = "Geeks"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Call check function check(str, pattern); // Change string str = "geeksforgeeks"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Calling check function check(str, pattern); </script>
Producción:
String = "GeeksforGeeks" String should begin with = "Geeks" String begins with "Geeks" String = "geeksforgeeks" String should begin with = "Geeks" String doesn't begins with "Geeks"
Método 2: En este método, usaremos la función substring() para obtener la substring de la longitud requerida de la string del patrón y luego uniremos la substring con el patrón usando la función localeCompare() .
Sintaxis:
var = string.substring(Startindex, Endindex) var.localeCompare(compareString)
Ejemplo: El siguiente programa demuestra el enfoque anterior:
<script> // 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 var sub_str = str.substring(0, pattern.length); if(sub_str.localeCompare(pattern) == 0){ document.write("String begins with \"" + pattern + "\"<br><br>"); } else{ document.write("String doesn't " + "begins with \"" + pattern + "\"<br><br>"); } } // Driver code // String to check var str = "GeeksforGeeks"; // Pattern by which string // begins or not var pattern = "Geeks"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Call check function check(str, pattern); // Change string str = "geeksforgeeks"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Calling check function check(str, pattern); </script>
Producción:
String = "GeeksforGeeks" String should begin with = "Geeks" String begins with "Geeks" String = "geeksforgeeks" String should begin with = "Geeks" String doesn't begins with "Geeks"
Método 3: esta es la mejor solución sobre todo, en este método usaremos el método beginWith() para verificar directamente si la string dada comienza con algo o no.
Sintaxis:
str.startsWith( searchString , position )
Ejemplo: El siguiente programa demuestra el enfoque anterior:
<script> // 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)){ document.write("String begins with \"" + pattern + "\"<br><br>"); } else{ document.write("String doesn't " + "begins with \"" + pattern + "\"<br><br>"); } } // Driver code // String to check var str = "Burn to shine"; // Pattern by which string // begins or not var pattern = "Burn"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Call check function check(str, pattern); // Change string str = "Happy coding"; // Change pattern pattern = "happy"; document.write("String = \"" + str + "\"<br>"); document.write("String should begin with = \"" + pattern + "\"<br>"); // Calling check function check(str, pattern); </script>
Producción:
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"