On this page本页内容
$indexOfCP¶New in version 3.4.版本3.4中的新功能。
Searches a string for an occurrence of a substring and returns the UTF-8 code point index (zero-based) of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8代码点索引(从零开始)。If the substring is not found, returns 如果未找到子字符串,则返回-1.-1。
$indexOfCP has the following operator expression syntax:具有以下运算符表达式语法:
<string> |
string |
|
<substring> |
string | |
<start> |
integer |
|
<end> |
integer |
|
If the 如果在<substring expression> is found multiple times within the <string expression>, then $indexOfCP returns the index of the first <substring expression> found from the starting index position.<string expression>中多次找到<substring expression>,则$indexOfCP返回从起始索引位置找到的第一个<substring expression>的索引。
$indexOfCP returns 返回null:null:
<string expression> is null, or<string expression>为空,或<string expression> refers to a non-existing field in the input document.<string expression>指的是输入文档中不存在的字段。$indexOfCP returns an error:返回一个错误:
<string expression> is not a string and not null, or<string expression>不是字符串且不为null,或<substring expression> is null or is not a string or refers to a nonexistent field in the input document, or<substring expression>为空或不是字符串或引用了输入文档中不存在的字段,或<start> or <end> is a negative integer (or a value that can be represented as a negative integer, like -5.0).<start>或<end>是负整数(或可以表示为负整数的值,如-5.0)。$indexOfCP returns 返回-1:-1:
<string expression>, or<string expression>中找不到子字符串,或<start> is a number greater than <end>, or<start>大于<end>,或<start> is a number greater than the byte length of the string.<start>是一个大于字符串字节长度的数字。{ $indexOfCP: [ "cafeteria", "e" ] } |
3 |
{ $indexOfCP: [ "cafétéria", "é" ] } |
3 |
{ $indexOfCP: [ "cafétéria", "e" ] } |
-1 |
{ $indexOfCP: [ "cafétéria", "t" ] } |
4 |
{ $indexOfCP: [ "foo.bar.fi", ".", 5 ] } |
7 |
{ $indexOfCP: [ "vanilla", "ll", 0, 2 ] } |
-1 |
{ $indexOfCP: [ "vanilla", "ll", -1 ] } |
Error |
{ $indexOfCP: [ "vanilla", "ll", 12 ] } |
-1 |
{ $indexOfCP: [ "vanilla", "ll", 5, 2 ] } |
-1 |
{ $indexOfCP: [ "vanilla", "nilla", 3 ] } |
-1 |
{ $indexOfCP: [ null, "foo" ] } |
null |
Consider an 考虑带有以下文档的inventory collection with the following documents:inventory集合:
The following operation uses the 以下操作使用$indexOfCP operator to return the code point index at which the string foo is located in each item string:$indexOfCP运算符返回每个item字符串中字符串foo所在的代码点索引:
The operation returns the following results:操作返回以下结果:
See also参阅