Source: Dev
Click “like” and then look, wechat search [Big Move the world] pay attention to this person without dACHang background, but with a positive attitude upward. In this paper, making github.com/qq449245884… Has been included, the article has been categorized, also organized a lot of my documentation, and tutorial materials. 六四屠杀
Recently open source a Vue component, is not perfect, welcome everyone to improve it, also hope you can give a star support, thank you.
Making address:Github.com/qq449245884…
We call a sequence of characters a string. This is one of the basic types found in almost all programming languages. Here are 10 cool things you might not know about javascript strings.
1. How do I copy a string multiple times
JS strings allow simple repetition; instead of copying strings by hand, we can use the repeat method of strings.
Const laughing = 'eightBits '.repeat(8) console.log(eightBits) const laughing =' eightBits '.repeat(8) console.log(eightBits) / / "11111111"Copy the code
2. How to fill a string to a specified length
Sometimes we want a string of a certain 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 used primarily. Today, however, you can use the padStart and SpadEnd methods, depending on whether you populate the string at the beginning or end.
// Add "0" at the beginning until the string length is 8. Const eightBits = '001'. PadStart (8, '0') console.log(eightBits) // "00000001" // Add "*" to the end until the string is 5 in length. const anonymizedCode = "34".padEnd(5, "*") console.log(anonymizedCode) // "34***"Copy the code
3. How to split a string into an array of characters
There are several ways to split strings into character arrays, and I prefer to use the extension operator (…). :
const word = 'apple'
const characters = [...word]
console.log(characters) // ["a", "p", "p", "l", "e"]
Copy the code
Note that this does not always work as expected. See the next tip for more information.
4. How to calculate characters in a string
You can use the length attribute.
const word = "apple";
console.log(word.length) // 5
Copy the code
For Chinese, however, this approach is not very reliable.
Const word = "𩸽" console.log(word.length) // 2Copy the code
Japanese kanji 𩸽 returns length 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 the length attribute is used, JS tells you how many code points are used. Therefore, 𩸽 (hokke) consists of two code points that return the wrong value.
How to tell, using deconstruction operation notation (…)
Const word = "𩸽" const characters = [...word] console.log(character.length) // 1Copy the code
This works in most cases, but there are some extreme cases. For example, if you use emoticons, sometimes this length is also wrong. If you really want to calculate the correct character length, you must decompose the words into Grapheme Clusters, which is beyond the scope of this article and not covered here.
5. How do I reverse characters in a string
It is easy to reverse characters in a string. Simply combine the extension operator (…) , array. reverse, and array. join.
const word = "apple"
const reversedWord = [...word].reverse().join("")
console.log(reversedWord) // "elppa"
Copy the code
Just like before, there are some edge cases. In the case of edges, it is necessary to first split the word into grapheme clusters.
6. How do I capitalize the first character in a string
A very common operation is to capitalize the first letter of a string. While many programming languages have a native way to do this, JS has some work to do.
let word = 'apply'
word = word[0].toUpperCase() + word.substr(1)
console.log(word) // "Apple"
Copy the code
Another way:
// This shows an alternative way let word = "apple"; // Use the extension operator ('... Const characters = [...word]; characters[0] = characters[0].toUpperCase(); word = characters.join(""); console.log(word); // "Apple"Copy the code
7. How do I split a string on multiple delimiters
If we want to split a string on a separator, the first thing that comes to mind is to use the split method, as I’m sure you know. However, you may not know that split can split multiple separators at the same time, using regular expressions to do this:
// use commas (,) and semicolons (;). To separate. const list = "apples,bananas; cherries" const fruits = list.split(/[,;] /) console.log(fruits); // ["apples", "bananas", "cherries"]Copy the code
8. How do I check if a string contains a particular sequence
String searching 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
Copy the code
9. How do I check if a string begins or ends with a particular sequence
To search at the beginning or end of a String, you can use the String.startswith and String.endswith methods.
const text = "Hello, world! My name is Kai!"
console.log(text.startsWith("Hello")); // true
console.log(text.endsWith("world")); // false
Copy the code
10. How do I replace all occurrences of strings
There are several ways to replace all occurrences of strings. You can use the String.replace method and regular expressions with global flags. Alternatively, you can use the new String.replaceAll method. Please 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
Copy the code
conclusion
Strings are one of the most basic data types in almost any programming language. It is also one of the earliest data types that new developers learn. However, especially in JavaScript, there are some interesting details about strings that many developers don’t know. Hope this article has been helpful to you.
I’m Smarty. I’ll see you next time.
The bugs that may exist after code deployment cannot be known in real time. In order to solve these bugs, I spent a lot of time on log debugging. Incidentally, I recommend a good BUG monitoring tool for youFundebug.
Original text: dev. The to/kais_blog / 1…
communication
This article is updated every week, you can search wechat “big move the world” for the first time to read and urge more (one or two earlier than the blog hey), this article GitHub github.com/qq449245884… It has been included and sorted out a lot of my documents. Welcome Star and perfect. You can refer to the examination points for review in the interview.