In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about what the JavaScript string is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
When it comes to strings, we are all too familiar. The first classic task of contact programming is to output strings: Hello, world. But do you know how JavaScript strings are represented in a computer?
The simplest intuitive but less accurate understanding is that a string is a sequence of characters such as English letters, numbers, and punctuation. For example, the following string consists of five letters and an exclamation point:
Const message = 'Hellobirds'
At the same time, you can see that the number of characters of the string is 6:
Const message = 'Hellograms; message.length; / / = > 6
If the string is made up of these visible characters (that is, 127 ASCII characters), there is no problem with this understanding. However, when you encounter unusual symbols, such as some emoji characters, you may get unexpected results:
Const smile ='?'; smile.length; / / = > 2
Isn't that weird? Obviously there is only one character, how can the length be 2? This is because the JavaScript string is actually made up of encoding units rather than a sequence of visible characters.
The ECMA 262specification describes JavaScript strings as follows:
The String type is an ordered sequence of zero or more 16-bit unsigned integer values. The string type is typically used to represent text data in a running ECMAScript program, in which case each element in the string is treated as an UTF-16 encoding unit value.
To put it simply, a JavaScript string is a sequence of UTF-16 encoding units, just a string of numbers.
An encoding unit is a number between 0x0000 and 0xFFFF, and there is a corresponding relationship between the encoding unit and the characters. For example, the encoding unit 0x0048 corresponds to the actual character H:
Const letter ='\ u0048mm; letter ='H' / / = > true
If you put an entire string of 'Hellobones' Expressed in terms of coding units, that's what it is:
Const message ='\ u0048\ u0065\ u006C\ u006C\ u006F\ u0021; message = = 'Hellograms; / / = > true message.length; / / = > 6
As you can see, this string has six encoding units, each corresponding to a character. Any character in the basic multilingual plane BMP (Basic Multilingual Plane) can be represented by a UTF-16 encoding unit. However, characters outside this range need to be represented by two UTF-16 encoding units. For example, the smiley face symbol mentioned earlier is encoded as\ uD83D\ uDE00:
Const smile ='\ uD83D\ uDE00'; smile = ='?'; / / = > true smile.length; / / = > 2
These two encoding units exist in pairs and are used to represent characters that exceed the 0xFFFF. You can't take it apart, or it will become unrecognizable garbled. In addition, the .length here is 2, indicating that this property is actually the number of string encoding units, not the number of characters. When you need to judge the number of characters, it should be noted that the results based on .length are inaccurate. So how to solve it? You can do it this way:
Const message = 'Hellograms; const smile ='?'; [... message] .length; / / = > 6 [... smile] .length; / / = > 1 is what the JavaScript string is. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.