SoFunction
Updated on 2025-03-09

Instructions for using the method

Method description:

Gets the byte length of the string.

The difference between this function is that the latter returns the number of characters of the string.

grammar:

Copy the codeThe code is as follows:

(string, [encoding])

Receive parameters:

string
encoding

example:

Copy the codeThe code is as follows:

str = '\u00bd + \u00bc = \u00be';
(str + ": " + + " characters, " +
  (str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes

Source code:

Copy the codeThe code is as follows:

= function(str, enc) {
  var ret;
  str = str + '';
  switch (enc) {
    case 'ascii':
    case 'binary':
    case 'raw':
      ret = ;
      break;
    case 'ucs2':
    case 'ucs-2':
    case 'utf16le':
    case 'utf-16le':
      ret = * 2;
      break;
    case 'hex':
      ret = >>> 1;
      break;
    default:
      ret = (str, enc);
  }
  return ret;
};