On this page本页内容
$substrCP
¶Returns the substring of a string. The substring starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string for the number of code points specified.
$substrCP
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 |
code point index |
number | Indicates the starting point of the substring. code point index can be any valid expression as long as it resolves to a non-negative integer. |
code point 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). |
{ $substrCP: [ "abcde", 1, 2 ] } |
"bc" |
{ $substrCP: [ "Hello World!", 6, 5 ] } |
"World" |
{ $substrCP: [ "cafétéria", 0, 5 ] } |
"cafét" |
{ $substrCP: [ "cafétéria", 5, 4 ] } |
"éria" |
{ $substrCP: [ "cafétéria", 7, 3 ] } |
"ia" |
{ $substrCP: [ "cafétéria", 3, 1 ] } |
"é" |
The $substrCP
operator uses the code points to extract the substring. This behavior differs from the $substrBytes
operator which extracts the substring by the number of bytes, where each character uses between one and four bytes.
Consider an inventory
collection with the following documents:
The following operation uses the $substrCP
operator to separate the quarter
value 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 $strLenCP
.
The operation returns the following results:操作返回以下结果:
A collection named food
contains the following documents:
The following example uses the $substrCP
operator to create a three byte menuCode
from the name
value:
The operation returns the following results:操作返回以下结果:
See also参阅