Prior discussion from 7 years ago: https://esdiscuss.org/topic/how-to-count-the-number-of-symbols-in-a-string
[...string].length does what you want. But it's definitely not always what you need <https://mathiasbynens.be/notes/javascript-unicode#other-grapheme-clusters>. On Thu, Aug 8, 2019 at 4:37 AM fanerge <[email protected]> wrote: > I expect to be able to add an attribute to String.prototype that returns > the number of codePoints of the string to reflect the actual number of > characters instead of the code unit. > > > Definition of String.prototype.length > > This property returns the number of code units in the string. UTF-16 > <https://en.wikipedia.org/wiki/UTF-16>, the string format used by > JavaScript, uses a single 16-bit code unit to represent the most common > characters, but needs to use two code units for less commonly-used > characters, so it's possible for the value returned by length to not > match the actual number of characters in the string. > > We refer to the String class in Java > > The String class in the Java JVM uses UTF-16 encoding. > String.length(): The method returns the number of characters in char in > the string; > String.codePointCount(): The method returns the number of codewords in > the string. > > > *I want the ECMA organization to be able to add a property or method to > String.prototype that returns the value of the codePoint of the string. For > example: String.prototype.codePointCount can return the actual number of > codePoints instead of code unit.* > > *```* > > const str1 = ‘1111’; > > str1.length; // 4 > > str1.codePointCount; // 4 > > // ‘1’.codePointAt(0) // 49 > > > const str2 = '𠮷𠮷𠮷𠮷’; > > str2.length; // 8 > > str2.codePointCount; // 4 > > // '𠮷'.codePointAt(0); // 134071 > > > const str3 = ‘😯😯😯😯’; > > str3.length; // 8 > > str3.codePointCount; // 4 > > // '😯'.codePointAt(0); // 128559 > > *```* > > *I believe that most developers need such a method and property to get the > number of codePoints in a string. I sincerely hope that you can accept my > proposal*,* thanks.* > > > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

