On this page本页内容
$substrBytes¶New in version 3.4.版本3.4中的新功能。
Returns the substring of a string. The substring starts with the character at the specified UTF-8 byte index (zero-based) in the string and continues for the number of bytes specified.
$substrBytes has the following operator expression syntax:
string expression |
string | The string from which the substring will be extracted. If the argument resolves to a value of If the argument does not resolve to a string or |
byte index |
number | Indicates the starting point of the substring.
|
byte count |
number | Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer (such as 2.0).
|
The $substrBytes operator uses the indexes of UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode.
For example, US-ASCII characters are encoded using one byte. Characters with diacritic markings and additional Latin alphabetical characters (i.e. Latin characters outside of the English alphabet) are encoded using two bytes. Chinese, Japanese and Korean characters typically require three bytes, and other planes of unicode (emoji, mathematical symbols, etc.) require four bytes.
It is important to be mindful of the content in the string expression because providing a byte index or byte count located in the middle of a UTF-8 character will result in an error.
$substrBytes differs from $substrCP in that $substrBytes counts the bytes of each character, whereas $substrCP counts the code points, or characters, regardless of how many bytes a character uses.
Errors with message:
| |
Errors with message:
|
Consider an inventory collection with the following documents:
The following operation uses the $substrBytes operator separate the quarter value (containing only single byte US-ASCII characters) into a yearSubstring and a quarterSubstring. The quarterSubstring field represents the rest of the string from the specified byte index following the yearSubstring. It is calculated by subtracting the byte index from the length of the string using $strLenBytes.
The operation returns the following results:操作返回以下结果: