Šajā apmācībā jūs uzzināsit par JavaScript virkni, izmantojot piemērus.
JavaScript virkne ir primitīvs datu tips, ko izmanto darbam ar tekstiem. Piemēram,
const name = 'John';
Izveidojiet JavaScript virknes
JavaScript valodā virknes tiek izveidotas, apņemot tās ar pēdiņām. Ir trīs veidi, kā varat izmantot pēdiņas.
- Atsevišķas pēdiņas:
'Hello'
- Divkāršās pēdiņas:
"Hello"
- Aizmugure:
`Hello`
Piemēram,
//strings example const name = 'Peter'; const name1 = "Jack"; const result = `The names are $(name) and $(name1)`;
Atsevišķas pēdiņas un dubultās pēdiņas ir praktiski vienādas, un jūs varat izmantot jebkuru no tām.
Backticks parasti tiek izmantoti, ja virknē jāiekļauj mainīgie vai izteicieni. Tas tiek darīts, iesaiņojot mainīgos vai izteicienus, $(variable or expression)
kā parādīts iepriekš.
Citātu var ierakstīt arī citātā. Piemēram,
const name = 'My name is "Peter".';
Tomēr pēdiņai nevajadzētu sakrist ar apkārtējām pēdiņām. Piemēram,
const name = 'My name is 'Peter'.'; // error
Piekļūstiet virkņu rakstzīmēm
Virknes rakstzīmēm var piekļūt divējādi.
- Viens veids ir traktēt virknes kā masīvu. Piemēram,
const a = 'hello'; console.log(a(1)); // "e"
- Vēl viens veids ir izmantot metodi
charAt()
. Piemēram,
const a = 'hello'; console.log(a.charAt(1)); // "e"
JavaScript virknes nav maināmas
JavaScript valodā virknes nav maināmas. Tas nozīmē, ka virknes rakstzīmes nevar mainīt. Piemēram,
let a = 'hello'; a(0) = 'H'; console.log(a); // "hello"
Tomēr jūs varat piešķirt mainīgā nosaukumu jaunai virknei. Piemēram,
let a = 'hello'; a = 'Hello'; console.log(a); // "Hello"
JavaScript ir reģistrjutīgs
JavaScript ir reģistrjutīgs. Tas nozīmē, ka JavaScript mazie un lielie burti tiek uzskatīti par atšķirīgām vērtībām. Piemēram,
const a = 'a'; const b = 'A' console.log(a === b); // false
JavaScript a
un A
tiek uzskatītas par atšķirīgām vērtībām.
JavaScript daudzrindu virknes
Lai izmantotu daudzrindu virkni, varat izmantot +
operatoru vai operatoru. Piemēram,
// using the + operator const message1 = 'This is a long message ' + 'that spans across multiple lines' + 'in the code.' // using the operator const message2 = 'This is a long message that spans across multiple lines in the code.'
JavaScript virknes garums
Lai uzzinātu virknes garumu, varat izmantot iebūvēto length
rekvizītu. Piemēram,
const a = 'hello'; console.log(a.length); // 5
JavaScript virknes objekti
Izmantojot new
atslēgvārdu, varat arī izveidot virknes . Piemēram,
const a = 'hello'; const b = new String('hello'); console.log(a); // "hello" console.log(b); // "hello" console.log(typeof a); // "string" console.log(typeof b); // "object"
Piezīme . Ieteicams izvairīties no virknes objektu izmantošanas. Stīgu objektu izmantošana palēnina programmu.
JavaScript virkņu metodes
Šeit ir visbiežāk izmantotās JavaScript virkņu metodes:
Metode | Apraksts |
---|---|
charAt (indekss) | atgriež rakstzīmi norādītajā indeksā |
concat () | pievienojas divām vai vairāk stīgām |
aizvietot() | aizstāj virkni ar citu virkni |
sadalīt () | pārveido virkni virkņu masīvā |
substrāts (sākums, garums) | atgriež virknes daļu |
apakšvirkne (sākums, beigas) | atgriež virknes daļu |
šķēle (sākums, beigas) | atgriež virknes daļu |
toLowerCase () | returns the passed string in lower case |
toUpperCase() | returns the passed string in upper case |
trim() | removes whitespace from the strings |
includes() | searches for a string and returns a boolean value |
search() | searches for a string and returns a position of a match |
Example: JavaScript String Methods
const text1 = 'hello'; const text2 = 'world'; const text3 = ' JavaScript '; // concatenating two strings const result1 = text1.concat(' ', text2); console.log(result1); // "hello world" // converting the text to uppercase const result2 = text1.toUpperCase(); console.log(result2); // HELLO // removing whitespace from the string const result3 = text3.trim(); console.log(result3); // JavaScript // converting the string to an array const result4 = text1.split(); console.log(result4); // ("hello") // slicing the string const result5= text1.slice(1, 3); console.log(result5); // "el"
JavaScript String() Function
The String()
function is used to convert various data types to strings. For example,
const a = 225; // number const b = true; // boolean //converting to string const result1 = String(a); const result2 = String(b); console.log(result1); // "225" console.log(result2); // "true"
If you want to learn more about the string conversion, visit JavaScript Type Conversion.
Escape Character
You can use the backslash escape character to include special characters in a string. For example,
const name = 'My name is \'Peter\'.'; console.log(name);
Output
My name is 'Peter'.
In the above program, the same quote is included using .
Here are other ways that you can use :
Code | Output |
---|---|
" | include double quote |
\ | ietver atpakaļ slīpsvītru |
n | jauna līnija |
r | rakstatgriezes |
v | vertikālā cilne |
t | horizontālā cilne |
b | Backspace |
f | formas barība |