On this page本页内容
$substr
¶Deprecated since version 3.4:自3.4版以来已弃用:$substr
is now an alias for 现在是$substrBytes
.$substrBytes
的别名。
Returns a substring of a string, starting at a specified index position and including the specified number of characters. 返回字符串的子字符串,从指定的索引位置开始,包含指定数量的字符。The index is zero-based.该指数是以零为基础的。
$substr
has the following syntax:语法如下所示:
The arguments can be any valid expression as long as the first argument resolves to a string, and the second and third arguments resolve to integers. 参数可以是任何有效的表达式,只要第一个参数解析为字符串,第二个和第三个参数解析为整数。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If 如果<start>
is a negative number, $substr
returns an empty string ""
.<start>
是负数,$substr
返回一个空字符串""
。
If 如果<length>
is a negative number, $substr
returns a substring that starts at the specified index and includes the rest of the string.<length>
是负数,$substr
返回一个子字符串,该子字符串从指定的索引开始,并包含字符串的其余部分。
$substr
only has a well-defined behavior for strings of ASCII characters.仅对ASCII字符字符串具有定义良好的行为。
Consider an 考虑使用以下文档集合inventory
collection with the following documents:inventory
:
The following operation uses the 以下操作使用$substr
operator to separate the quarter
value into a yearSubstring
and a quarterSubstring
:$substr
运算符将季度值分为yearSubstring
和quarterSubstring
:
The operation returns the following results:操作返回以下结果: