In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what the string operation methods, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
String properties and methods
Strings are used to represent and manipulate sequences of characters. There are many string properties and methods. The following code examples are available for reference, including "matchAll" in ES2020 and "replaceAll" in ES2021.
Const str = Today is a nice day!; console.log (str.length); / / 20 console.log (str [2]); / / "d" console.log (typeof str); / / "string" console.log (typeof str [2]); / / "string" console.log (typeofString (5)); / / "string" console.log (typeofnewString (str)) / / "object" console.log (str.indexOf (is)); / 6 console.log (str.indexOf (today)); / /-1 console.log (str.includes (is)); / / true console.log (str.includes (IS)); / / false console.log (str.startsWith (Today)); / / true console.log (str.endsWith (day)) / / false console.log (str.split ()); / / ["Today", "is", "a", "nice", "day!"] Console.log (str.split ()); / ["T", "o", "d", "a", "y", "", "I", "s", "," a ","n", "I", "c", "e", "", "d", "a", "y", "!"] Console.log (str.split (a)); / / ["Tod", "y is", "nice d", "y!"] Console.log (str + 1x2); / / "Today is a niceday! 12" console.log (str + str); / / "Today is a niceday! Today is a niceday!" Console.log (str.concat (str)); / / "Today is a niceday! Today is a niceday!" Console.log (str.repeat (2)); / / "Today is a nice daylight Today is a nice day!" Console.log (abc
< bcd ); // true console.log( abc .localeCompare( bcd )); // -1 console.log( a .localeCompare( A )); // -1 console.log( a .localeCompare( A , undefined, { numeric: true })); // -1 console.log( a .localeCompare( A , undefined, { sensitivity: accent })); // 0 console.log( a .localeCompare( A , undefined, { sensitivity: base })); // 0 console.log( a .localeCompare( A! , undefined, { sensitivity: base , ignorePunctuation: true })); // 0 console.log( abc .toLocaleUpperCase()); // "ABC" console.log(str.padStart(25, * )); // "*****Todayis a nice day!" console.log(str.padEnd(22, ! )); // "Today is anice day!!!" console.log( middle .trim().length); // 6 console.log( middle .trimStart().length); // 8 console.log( middle .trimEnd().length); // 9 console.log(str.slice(6, 8)); // "is" console.log(str.slice(-4)); // "day!" console.log(str.substring(6, 8)); // "is" console.log(str.substring(-4)); // "Today is a nice day!" console.log( a .charCodeAt()); // 97 console.log(String.fromCharCode(97)); // "a" console.log(str.search(/[a-c]/)); // 3 console.log(str.match(/[a-c]/g)); // ["a", "a", "c", "a"] console.log([...str.matchAll(/[a-c]/g)]); // [Array(1), Array(1), Array(1), Array(1)] // 0: ["a", index: 3, input: "Today is a nice day!",groups: undefined] // 1: ["a", index: 9, input: "Today is a nice day!",groups: undefined] // 2: ["c", index: 13, input: "Today is a niceday!", groups: undefined] // 3: ["a", index: 17, input: "Today is a niceday!", groups: undefined] console.log([... test1test2 .matchAll(/t(e)(st(d?))/g)]); // [Array(4), Array(4)] // 0: (4) ["test1", "e", "st1","1", index: 0, input: "test1test2", groups: undefined] // 1: (4) ["test2", "e", "st2","2", index: 5, input: "test1test2", groups: undefined] console.log(str.replace( a , z )); // Todzy is anice day! console.log(str.replace(/[a-c]/, z )); // Todzy is anice day! console.log(str.replace(/[a-c]/g, z )); // Todzy is znize dzy! console.log(str.replaceAll( a , z )); // Todzy is znice dzy! console.log(str.replaceAll(/[a-c]/g, z )); // Todzy is znize dzy! console.log(str.replaceAll(/[a-c]/, z )); // TypeError:String.prototype.replaceAll called with a non-global RegExp argument 映射和集合 对于字符串操作,我们需要在某处存储中间值。数组、映射和集合都是需要掌握的常用数据结构,本文主要讨论集合和映射。 (1) 集合 Set是存储所有类型的唯一值的对象。以下是供参考的代码示例,一目了然。 const set =newSet( aabbccdefghi ); console.log(set.size); // 9 console.log(set.has( d )); // true console.log(set.has( k )); // false console.log(set.add( k )); // {"a", "b", "c", "d","e" "f", "g", "h", "i","k"} console.log(set.has( k )); // true console.log(set.delete( d )); // true console.log(set.has( d )); // false console.log(set.keys()); // {"a", "b", "c","e" "f", "g", "h", "i","k"} console.log(set.values()); // {"a", "b", "c","e" "f", "g", "h", "i","k"} console.log(set.entries()); // {"a" =>"a", "b" = > "b", "c" = > "c", "e" = > "e", / / "f" = > "f", "g" = > "g", "h" = > "h"}, "I" = > "I", "k" = > "k"} const set2 = newSet () Set.forEach (item = > set2.add (item.toLocaleUpperCase (); set.clear (); console.log (set); / / {} console.log (set2) / {"A", "B", "C", "E", "F", "G", "H", "I", "K"} console.log (newSet ([{a: 1, b: 2, c: 3}, {d: 4, e: 5}, {d: 4, e: 5}]) / {{a: 1, b: 2 const item c: 3}, {d: 4, e: 5}, {d: 4, e: 5}} const item = {f: 6, g: 7}; console.log ([{a: 1, b: 2 ~ c: 3}, item, item]) / / {{a: 1, b: 2,c: 3}, {f: 6, g: 7}}
(2) Mapping
A mapping is an object that holds key-value pairs. Any value can be used as a key or value. The mapping remembers the original insertion order of the keys. The following is a code example for reference:
Const map = newMap (); console.log (map.set (1, first)); / / {1 = > "first"} console.log (map.set (a, second)); / / {1 = > "first", "a" = > "second"} console.log (map.set ({obj: 123}, [1,2,3])) / / {1 = > "first", "a" = > "second", {obj: "123"} = > [1,2,3]} console.log (map.set ([2,2,2], newSet (abc) / / {1 = > "first", "a" = > "second", {obj: "123"} = > [1,2,3], [2,2,2] = > {"a", "b", "c"}} console.log (map.size); / / 4 console.log (map.has (1)); / / true console.log (map.get (1)) / / "first" console.log (map.get (a)); / / "second" console.log (map.get ({obj: 123})); / / undefined console.log (map.get ([2,2,2])); / / undefined console.log (map.delete (1)); / / true console.log (map.has (1)); / / false const arr = [3,3] Map.set (arr, newSet (xyz)); console.log (map.get (arr)); / / {"x", "y", "z"} console.log (map.keys ()); / / {"a", {obj: "123"}, [2Liao 2,2], [3,3]} console.log (map.values ()) / {"second", [1,2,3], {"a", "b", "c"}, {"x", "y", "z"}} console.log (map.entries ()) / {"a" = > "second", {obj: "123"} = > [1,2,3], [2,2,2] = > {"a", "b", "c"}, [3,3] = > {"x", "y", "z"}} const map2 = newMap ([[a, 1], [b, 2], [c, 3]]) Map2.forEach ((value, key, map) = > console.log (`value = ${value}, key = ${key}, map = ${map.size} `); / / value = 1, key = a, map = 3 / / value = 2, key = b, map = 3 / / value = 3, key = c, map = 3 map2.clear (); console.log (map2.entries ()); / / {}
Application problem
There are English application questions in the interview, and we have explored some algorithms that are often used in testing.
(1) isoline
An isoline map is a word that contains letters that appear only once.
Dermatoglyphics (15 letters)
Hydropneumatics (15 letters)
Misconjugatedly (15 letters)
Uncopyrightable (15 letters)
Uncopyrightables (16 letters)
Subdermatoglyphic (17 letters)
How to write an algorithm to detect whether a string is an isoline map? There are many ways to do this. You can put a string in a collection and then automatically split it into characters. Because the collection is an object that stores unique values, if it is an isoline map, its size should be the same as the length of the string.
/ * * An algorithm to verify whethera given string is an isogram * @ param {string} str The string to be verified * @ return {boolean} Returns whether it is an isogram * / functionisIsogram (str) {if (! str) {returnfalse;} const set = newSet (str); return set.size=== str.length;}
The following is the verification test:
Console.log (isIsogram ()); / false console.log (isIsogram (a)); / / true console.log (isIsogram (misconjugatedly)); / / true console.log (isIsogram (misconjugatledly)); / / false
(2) all-letter short sentences
An all-letter short sentence is a sentence that contains all 26 letters in the alphabet, regardless of case. Ideally, the shorter the sentence, the better. The following are all-letter short sentences:
Waltz, bad nymph, for quick jigs vex. (28 letters)
Jived fox nymph grabs quick waltz. (28 letters)
Glib jocks quiz nymph to vex dwarf. (28 letters)
Sphinx of black quartz, judge my vow. (29 letters)
How vexingly quick daft zebras jump! (30 letters)
The five boxing wizards jump quickly. (31 letters)
Jackdaws love my big sphinx of quartz. (31 letters)
Pack my box with five dozen liquor jugs. (32 letters)
The quick brown fox jumps over a lazy dog. (33 letters)
There are many ways to verify that a given string is an all-letter phrase. This time, we put each letter (converted to lowercase) into the mapping. If the mapping size is 26, then it is an all-letter short sentence.
/ * An algorithm to verify whethera given string is a pangram * @ param {string} str The string to be verified * @ return {boolean} Returns whether it is a pangram * / functionisPangram (str) {const len = str.length; if (len maxPalindrome.length) {/ / if it is longerthan the current length maxPalindrome = lettersAndDigits.slice (I-j, I + j + 1) / / j steps before, middle, and j steps after}} / / try the case that the palindrome has two middles if (I
< len -1&& str[i] === str[i +1]) { // if two middles are the same if (maxPalindrome.length= 0&&// cannot pass the start index (inclusive) i + j +1< len &&// cannot exceed end index (exclusive) Math.min(2 * i +2, 2 * (len - i)) >MaxPalindrome.length; / / potential max length should be longer than thecurrent length jacks +) {if (str [I-j]! = = str [I + j + 1]) {/ / if j stepsbefore the left middle is different from j steps after the right middle break } if (2 * j + 2 > maxPalindrome.length) {/ / if it is longer than the current length maxPalindrome = lettersAndDigits.slice (I-j, I + j + 2); / / j steps before, middles, and j steps after} return maxPalindrome;}
The following is the verification test:
Console.log (findLongestPalindrome ()); / / "" console.log (findLongestPalindrome (abc)); / / "a" console.log (findLongestPalindrome (Aabcd)); / / "Aa" console.log (findLongestPalindrome (I am Bob. ); / / "Bob" console.log (findLongestPalindrome (Odd oreven)); / / "Oddo" console.log (findLongestPalindrome (Neveroddoreven)); / / "Neveroddoreven" console.log (findLongestPalindrome (Today is 02 engine 02). )); / / "02022020" console.log (findLongestPalindrome (It is 2) 20 max 2020. ); / / "20202" console.log (findLongestPalindrome (Aman, aplan, acanal-Panama)); / / "AmanaplanacanalPanama" is all the contents of the article "how to manipulate strings". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.