Programma JavaScript per verificare se una stringa inizia e finisce con determinati caratteri

In questo esempio imparerai a scrivere un programma JavaScript per verificare se una stringa inizia e finisce con determinati caratteri.

Per comprendere questo esempio, è necessario conoscere i seguenti argomenti di programmazione JavaScript:

  • Stringa JavaScript
  • La stringa JavaScript inizia con ()
  • La stringa JavaScript finisce con ()
  • JavaScript Regex

Esempio 1: controllare la stringa utilizzando metodi incorporati

 // program to check if a string starts with 'S' and ends with 'G' function checkString(str) ( // check if the string starts with S and ends with G if(str.startsWith('S') && str.endsWith('G')) ( console.log('The string starts with S and ends with G'); ) else if(str.startsWith('S')) ( console.log('The string starts with S but does not end with G'); ) else if(str.endsWith('G')) ( console.log('The string starts does not with S but end with G'); ) else ( console.log('The string does not start with S and does not end with G'); ) ) // take input let string = prompt('Enter a string: '); checkString(string);

Produzione

 Immettere una stringa: String La stringa inizia con S ma non finisce con G

Nel programma sopra, vengono utilizzati i due metodi startsWith()e endsWith().

  • Il startsWith()metodo controlla se la stringa inizia con la stringa specifica.
  • Il endsWith()metodo controlla se la stringa termina con la stringa specifica.

Il programma precedente non controlla le lettere minuscole. Quindi, qui G e g sono diversi.

Si potrebbe anche verificare se il carattere inizia sopra con S o s e termina con G o g .

 str.startsWith('S') || str.startsWith('s') && str.endsWith('G') || str.endsWith('g');

Esempio 2: controllare la stringa utilizzando Regex

 // program to check if a string starts with 'S' and ends with 'G' function checkString(str) ( // check if the string starts with S and ends with G if( /^S/i.test(str) && /G$/i.test(str)) ( console.log('The string starts with S and ends with G'); ) else if(/^S/i.test(str)) ( console.log('The string starts with S but does not ends with G'); ) else if(/G$/i.test(str)) ( console.log('The string starts does not with S but ends with G'); ) else ( console.log('The string does not start with S and does not end with G'); ) ) // for loop to show different scenario for (let i = 0; i < 3; i++) ( // take input const string = prompt('Enter a string: '); checkString(string); )

Produzione

 Immettere una stringa: Stringa La stringa inizia con S e finisce con G Immettere una stringa: stringa La stringa inizia con S e finisce con G Immettere una stringa: JavaScript La stringa non inizia con S e non finisce con G

Nel programma precedente, un'espressione regolare (RegEx) viene utilizzato con il test()metodo per verificare se la stringa inizia con S e termina con G .

  • Il /^S/imodello controlla se la stringa è S o s . Qui, idenota che la stringa non fa distinzione tra maiuscole e minuscole. Quindi, S e s sono considerati uguali.
  • Il /G$/ipattern controlla se la stringa è G o g .
  • L' if… else… ifistruzione viene utilizzata per verificare le condizioni e visualizzare il risultato di conseguenza.
  • Il forloop viene utilizzato per ricevere input diversi dall'utente per mostrare casi diversi.

Articoli interessanti...