In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are 10 great JavaScript string skills? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
We call a character sequence as a string. This is one of the basic types found in almost all programming languages.
1. How to copy a string multiple times
JS strings allow simple repetition, and unlike copying strings by hand, we can use the repeat method of strings.
Const laughing = '1'.repeat'. Repeat (3) consol.log (laughing) / / const eightBits = '1'.repeat (8) console.log (eightBits) / / "11111111"
two。 How to fill a string to a specified length
Sometimes we want the string to have a specific length. If the string is too short, the remaining space needs to be filled until the specified length is reached.
In the past, the library left-pad was mainly used. Today, however, we can use the padStart and SpadEnd methods, depending on whether to populate the string at the beginning or end of the string.
/ / add "0" at the beginning until the length of the string is 8.
/ / add "0" at the beginning until the length of the string is 8. Const eightBits = '001'.padStart (8,' 0') console.log (eightBits) / / "00000001" / / add "*" at the end until the length of the string is 5. Const anonymizedCode = "34" .padEnd (5, "*") console.log (anonymizedCode) / / "34 clients *"
3. How to split a string into an array of characters
There are several ways to split a string into an array of characters, and I prefer to use the extension operator (.):
Const word = 'apple' const characters = [... word] console.log (characters) / / ["a", "p", "p", "l", "e"]
Note that this does not always work as expected. For more information, see the next tip.
4. How to calculate characters in a string
You can use the length property.
Const word = "apple"; console.log (word.length) / / 5
But for Chinese, this method is not very reliable.
Const word = "?" Console.log (word.length) / / 2
Japanese Kanji? The return length is 2. Why? JS represents most characters as 16-bit code points. However, some characters are represented as two (or more) 16-bit code points, called proxy pairs. If you are using the length attribute, JS tells you how many code points are used. Therefore, hokke consists of two code points and returns the wrong value.
Then how to judge, using the deconstruction operation symbol (.)
Const word = "?" Const characters = [... word] console.log (characters.length) / / 1
This method works in most cases, but there are some extreme cases. For example, if you use emoticons, sometimes this length is wrong. If you really want to calculate the correct length of a character, you must decompose the word into Grapheme Clusters, which is beyond the scope of this article, and is not explained here.
5. How to reverse characters in a string
It is easy to reverse the characters in a string. Just combine the extension operator (...), the Array.reverse method, and the Array.join method.
Const word = "apple" const reversedWord = [. Word] .reverse () .join ("") console.log (reversedWord) / / "elppa"
As before, there are some marginal situations. When you encounter an edge situation, it is necessary to split the word into morpheme clusters first.
6. How to capitalize the first letter in a string
A very common operation is to capitalize the first letter of a string. Although many programming languages have a native way to do this, JS needs to do some work.
Let word = 'apply' word = word [0] .toUpperCase () + word.substr (1) console.log (word) / / "Apple"
Another way:
/ / This shows an alternative way let word = "apple"; / / use the extension operator (`...`) to split the characters const characters = [... word]; characters [0] = characters [0] .toUpperCase (); word = characters.join ("); console.log (word); / /" Apple "
7. How to split a string on multiple delimiters
If we want to split the string on the delimiter, the first thing that comes to mind is to use the split method. Zhimi must know this. What you may not know, however, is that split can split multiple delimiters at the same time, which can be achieved using regular expressions:
/ / separate by comma (,) and semicolon (;). Const list = "apples,bananas;cherries" const fruits = list.split (/ [,;] /) console.log (fruits); / / ["apples", "bananas", "cherries"]
8. How to check whether a string contains a specific sequence
String search is a common task. In JS, you can easily do this using the String.includes method. Regular expressions are not required.
Const text = "Hello, world! My name is Kai!" Console.log (text.includes ("Kai")); / / true
9. How to check whether a string begins or ends with a specific sequence
You can use the String.startsWith and String.endsWith methods to search at the beginning or end of a string.
Const text = "Hello, world! My name is Kai!" Console.log (text.startsWith ("Hello")); / / true console.log (text.endsWith ("world")); / / false
10. How to replace all strings that appear
There are several ways to replace all strings that appear. You can use the String.replace method and regular expressions with global flags. Alternatively, you can use the new String.replaceAll method. Note that this new method is not available in all browsers and Node.js versions.
Const text = "I like apples. You like apples." Console.log (text.replace (/ apples/g, "bananas"); / / "I like bananas. You like bananas." Console.log (text.replaceAll ("apples", "bananas")); / / "I lik
String is one of the most basic data types in almost all programming languages. At the same time, it is also one of the earliest data types that new developers learn. However, especially in JavaScript, many developers don't know some interesting details about strings.
After reading the above, have you mastered 10 great JavaScript string techniques? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.