JavaScript programma, lai pārbaudītu, vai virkne sākas un beidzas ar noteiktām rakstzīmēm

Šajā piemērā jūs iemācīsities rakstīt JavaScript programmu, lai pārbaudītu, vai virkne sākas un beidzas ar noteiktām rakstzīmēm.

Lai saprastu šo piemēru, jums jāpārzina šādas JavaScript programmēšanas tēmas:

  • JavaScript virkne
  • Javascript virkne sākas ar ()
  • Javascript virkne beidzas ar ()
  • JavaScript regex

1. piemērs: pārbaudiet virkni, izmantojot iebūvētās metodes

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

Rezultāts

 Ievadiet virkni: virkne Virkne sākas ar S, bet nebeidzas ar G

Iepriekš minētajā programmā tiek izmantotas abas metodes startsWith()un endsWith().

  • Par startsWith()metode pārbauda, vai virkne sākas ar konkrētu virkni.
  • Par endsWith()metode pārbauda, vai virknes beidzas ar konkrētu virkni.

Iepriekš minētā programma nepārbauda mazos burtus. Tādējādi šeit G un g ir atšķirīgi.

Varat arī pārbaudīt, vai iepriekšminētā rakstzīme sākas ar S vai s un beidzas ar G vai g .

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

2. piemērs: pārbaudiet virkni, izmantojot 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); )

Rezultāts

 Ievadiet virkni: virkne Virkne sākas ar S un beidzas ar G Ievadiet virkni: virkne Virkne sākas ar S un beidzas ar G Ievadiet virkni: JavaScript Virkne nesākas ar S un nebeidzas ar G

Iepriekš programmā, regulāra izteiksme (RegEx) tiek izmantots kopā ar test()metodi, lai pārbaudītu, vai virkne sākas ar S un beidzas ar G .

  • Šīs /^S/imodelis pārbauda, ja virkne ir S vai s . Šeit iapzīmē, ka virkne nav reģistrjutīga. Tādējādi S un s tiek uzskatīti par vienādiem.
  • Par /G$/imodeļiem pārbauda, ja virkne ir G vai g .
  • if… else… ifApgalvojums tiek izmantots, lai pārbaudītu apstākļus un parādīt rezultātus atbilstoši.
  • forCilpa tiek izmantota, lai dažādās izejvielas no lietotāja, lai parādītu dažādas lietas.

Interesanti raksti...