Holly’s Code Corner — Епизод едно!
Низът е поредица от един или повече знаци, която може да се състои от букви, цифри или символи.
Пример: „Здравей свят“, „Abdhfoefhe“, „Днес ходих на плаж!“, „245“
1. За да намерите конкретен знак от низ
Пример #1: За да получите първата буква от низ.
const word = "basic"; const letter = word.charAt(0); console.log(letter); /* This would print "b" */
Пример #2: За да получите първата буква от низ.
const word = "basic"; const letter = word[0]; console.log(letter); /* This would print "b" */
Резюме: string.charAt() или string[index]
2. За разделяне на низ в масив
Пример#1: Разделяне на низ в масив.
const word = "I went to the beach today!"; let array = word.split(" "); console.log(array); /* This would print [ 'I', 'went', 'to', 'the', 'beach', 'today!' ] */
Пример #2: Разделяне на низ в масив по дума в низа.
const word = "I went to the beach today!"; let array = word.split("the"); console.log(array); /* This would print [ 'I went to ', ' beach today!' ] */ /* Notice that the word "the" is not in the array anymore.*/
Резюме: string.split(“разделител”)
3. Да превърнете масив в низ
Пример#1 За превръщане на масив в низ.
const array = ["I", "went", "to", "the", "beach", "today!"]; const word = array.join(" "); console.log(word); /* This would print "I went to the beach today!" */ const word2 = array.join(""); console.log(word); /* This would print "Iwenttothebeachtoday!" */
Резюме: string.join(“разделител”)
4. За обръщане на низ
(За съжаление, .reverse()не работи с низове)
Пример#1: Използване на "split, reverse, join" за обръщане на низ.
const word = "hello"; const wordArray = word.split(""); /* To turn a string into an array */ /* This would return [ 'h', 'e', 'l', 'l', 'o' ] */ const revWordArray = wordArray.reverse(); /* To reverse an array */ /* This would return [ '0', 'l', 'l', 'e', 'h' ] */ const revWordArrayToStirng = revWordArray.join(""); /* To reverse a string */ /* This would return "olleh" */
Резюме: string.split(“”).reverse().join(“”)
Пример#2 За обръщане на всяка дума в низ.
const word = "Have a great day!"; const wordToArr = word.split(" "); console.log(wordToArr); /*To turn a string into an array*/ /* This would return [ 'Have', 'a', 'great', 'day!'] */ const newArr = wordToArr.map(el => el.split("").reverse().join("")) console.log(newArr); /*To reverse every element in the wordToArr array*/ /* This would return [ 'evaH', 'a', 'taerg', '!yad' ] */ const result = newArr.join(" ") /*To turn an array into a string*/ /* This would return "evaH a taerg !yad" */
Резюме: Първо, разделете низа в масив.
След това използвайте метода на картата в масива.
array.map((дума) =› word.split(“”).reverse().join(“”)).join(“ ”)
5. Да превърнете низ в число
Пример: За да превърнете низ в число (обърнете внимание, че низът трябва да е валидно число)
const word ="123"; const num = parseInt(word) console.log(num) /* This would print 123 */
Резюме: parseInt(низ)
6. За замяна на дума или буква в низ
Пример №1
const word = "Hello World" const newWord = word.replace("Hello", "Hi"); console.log(newWord); /* This would print "Hi World" */
Пример #2 (За замяна на част от дума в низ)
let word = "kneggow"; const newWord = word.replace("egg", ""); console.log(newWord); /* This would print "know" */
Резюме: string.replace(“част, която искате да замените” “замяна”)
Пример #3 (За замяна на първата буква на всяка дума в низ)
const words = "Hello World" const wordToArr = words.split(" "); console.log(wordToArr); /* This would print [ 'Hello', 'World' ]*/ const newWord = wordToArr.map((word) => word.charAt[0] = "T" + word.slice(1))); console.log(newWord); /* This would print [ 'Tello', 'Torld' ]*/ /* Using .map function to replace the first charcter of each word in the array.*/ const result = newWord.join(" ") console.log(result); /* This would print "Tello Torld" ]*/
Резюме: Първо, разделете низа в масив.
Второ, използвайте метода на картата, за да замените първия знак.
След това използвайте метода на парче, за да запазите останалите знаци в думата.
И накрая, използвайте метода за присъединяване, за да превърнете масив в низ.
7. За да замените определени букви в низ с помощта на регулярен израз
Пример #1: Да премахнете думата „яйце“ от всички думи в низ
const word = "kneggowegg keggnegg keggneggoegg keggneggoeggwegg"; const newWord = word.replace(/egg/g, "") console.log(newWord); /* This would print "know kn kno know" */
Пример #2: Премахване на първото „яйце“ от низа
const newWord2 = word.replace(/egg/, "") console.log(newWord2); /* This would print "knowegg keggnegg keggneggoegg keggneggoeggwegg" */ /* Without the "g" after /egg/, the method will only replace the first "egg" it encounters.*/ /* More on Regex in the following section */
Още малко за RegEx
/* /(word or character or you would like to find)\w/ g "w" here stands for word(any word character) "g" here stands for global. */ const word = "kneggowegg, keggnegg ! keggneggoegg! keggneggoeggwegg"; 1) word.replace(/egg/, "hi") will replace the first "egg" in the string with "hi" /* console.log(word.replace(/egg/, "hi")) would print "knhiowegg, keggnegg ! keggneggoegg! keggneggoeggwegg" */ 2) word.replace(/egg/g, "hi") will replace all the "egg" in the string with "hi" /* console.log(word.replace(/egg/g, "hi")) would print "knhiowhi, khinhi ! khinhiohi! khinhiohiwhi" */ 3) word.replace(/\w/g, "hi") will replace every letter with "hi" /* console.log(word.replace(/\w/g, "hi")) would print "hihihihihihihihihihi, hihihihihihihihi ! hihihihihihihihihihihihi! hihihihihihihihihihihihihihihihi" */ 4) word.replace(/\w+/g, "hi") will replace every word with "hi" /* console.log(word.replace(/\w+/g, "hi")) would print "hi, hi ! hi! hi" */ 5)word.replace(/[a-g]/g, "hi") will replace all the letters from "a" to "g" with "hi"; /* it is case sensitive */ /* console.log(word.replace(/[a-g]/g, "hi")) will print "knhihihiowhihihi, khihihinhihihi ! khihihinhihihiohihihi! khihihinhihihiohihihiwhihihi" */ 6) word.replace(/[A-G]/g, "hi") will replace all the letters from "A" to "G" with "hi"; /* console.log(word.replace(/[A-G]/g, "hi")) will print "kneggowegg, keggnegg ! keggneggoegg! keggneggoeggwegg" */ /* Because there is no capita letter in the string word */
RegEx е цяла тема сама по себе си, моля, върнете се по-късно за следващата част от Holly’s Code Corner!🌻