JavaScript-programma om te controleren of een tekenreeks begint en eindigt met bepaalde tekens

In dit voorbeeld leer je een JavaScript-programma te schrijven om te controleren of een string begint en eindigt met bepaalde karakters.

Om dit voorbeeld te begrijpen, moet u kennis hebben van de volgende JavaScript-programmeeronderwerpen:

  • JavaScript-tekenreeks
  • Javascript String startsWith ()
  • Javascript String endsWith ()
  • JavaScript Regex

Voorbeeld 1: Controleer tekenreeks met behulp van ingebouwde methoden

 // 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);

Uitvoer

 Voer een string in: String De string begint met S maar eindigt niet met G

In het bovenstaande programma worden de twee methoden startsWith()en endsWith()gebruikt.

  • De startsWith()methode controleert of de string begint met de betreffende string.
  • De endsWith()methode controleert of de string eindigt met de betreffende string.

Het bovenstaande programma controleert niet op kleine letters. Daarom zijn G en g hier verschillend.

Je zou ook kunnen controleren of het bovenstaande teken begint met S of s en eindigt met G of g .

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

Voorbeeld 2: Controleer de tekenreeks met behulp van 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); )

Uitvoer

 Voer een string in: String De string begint met S en eindigt met G Voer een string in: string De string begint met S en eindigt met G Voer een string in: JavaScript De string begint niet met S en eindigt niet met G

In het bovenstaande programma wordt een reguliere expressie (RegEx) gebruikt bij de test()werkwijze te controleren of de string begint met S en eindigt met G .

  • Het /^S/ipatroon controleert of de string S of s is . Geeft hier iaan dat de tekenreeks niet hoofdlettergevoelig is. Daarom worden S en s als hetzelfde beschouwd.
  • Het /G$/ipatroon controleert of de string G of g is .
  • De if… else… ifverklaring wordt gebruikt om de voorwaarden te controleren en het resultaat dienovereenkomstig weer te geven.
  • De forlus wordt gebruikt om verschillende invoer van de gebruiker te ontvangen om verschillende gevallen te laten zien.

Interessante artikelen...