This time I will bring you the method of dynamically obtaining the number of bytes and characters of the current input content, and dynamically obtaining the number of bytes and characters of the current input content. What are the precautions?. The following is a practical case. Get up and take a look.
Detailed example of dynamically counting the number of bytes and characters of the current input content
I saw that there are many methods using charAt on the Internet. The principle is also very simple. Use regular expressions to determine whether it is Chinese. If so, add 2 to the number of bytes. If not, add 1 to the number of bytes.
Later I found another way. Since one Chinese character corresponds to two bytes, you can use regular rules to replace the matched Chinese character with two characters, for example: "xx", and finally get the converted string## The length of # is the number of bytes.
The number of characters corresponds to how many Chinese characters, letters, and symbols there are.//短信内容字数 $scope.bytesCount= 0; $scope.smsLength = 0; $scope.smsContent = '1'; $scope.smsCount = function () { $scope.bytesCount = $scope.smsContent.replace(/[^\x00-\xff]/g, 'xx').length; $scope.smsLength = $scope.smsContent.length; }
Double linked list definition of JS data structure
The above is the detailed content of Dynamically obtain the number of bytes and characters of the current input content. For more information, please follow other related articles on the PHP Chinese website!