Home  >  Article  >  Web Front-end  >  Dynamic statistics of the number of bytes and characters of the current input content

Dynamic statistics of the number of bytes and characters of the current input content

小云云
小云云Original
2018-01-20 10:47:211610browse

This article mainly introduces the relevant information on the detailed examples of dynamic statistics of the number of bytes and characters of the current input content. I hope this article can help everyone to realize such a function. Friends who need it can refer to it. I hope it can help. to everyone.

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 The regular expression determines whether it is Chinese. If so, the number of bytes is increased by 2. If not, the number of bytes is increased by 1.

Later I found another way. Since one Chinese character corresponds to two bytes, you can use regular rules to replace the matched Chinese characters with two characters, for example: "xx", and finally get the converted characters. The length of the string 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;

  }

Related recommendations:

How to implement bootstrap table sum total quantity statistics

PHP counts the number of times a number appears in a sorted array

php performs website visit information statistics

The above is the detailed content of Dynamic statistics of the number of bytes and characters of the current input content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn