9.4. String Functions and Operators
9.4. String Functions and Operators
9.4.字符串函数和运算符
This section describes functions and operators for examining and manipulating string values. Strings in this context include values of the types character, character varying, and text. Unless otherwise noted, all of the functions listed below work on all of these types, but be wary of potential effects of automatic space-padding when using the character type. Some functions also exist natively for the bit-string types.
本节介绍了用于检查和操作字符串值的函数和运算符。 此处,字符串包括character, character varying和text类型的值。 除非另有说明,否则下面列出的所有函数均可在所有这些类型上使用,但请注意在使用字符类型时自动空格填充的潜在影响。 还有一些专门针对位串类型的函数。
SQL defines some string functions that use key words, rather than commas, to separate arguments. Details are in Table 9.8. PostgreSQL also provides versions of these functions that use the regular function invocation syntax (see Table 9.9).
SQL定义了一些字符串函数,这些函数使用关键字而不是逗号来分隔参数。 详情见表9.8。 PostgreSQL还提供了使用常规函数调用语法的这些函数的版本(请参见表9.9)。
Note
注
Before PostgreSQL 8.3, these functions would silently accept values of several nonstring data types as well, due to the presence of implicit coercions from those data types to text. Those coercions have been removed because they frequently caused surprising behaviors. However, the string concatenation operator (||) still accepts non-string input, so long as at least one input is of a string type, as shown in Table 9.8. For other cases, insert an explicit coercion to text if you need to duplicate the previous behavior.
在PostgreSQL 8.3之前,由于存在从这些数据类型到文本的隐式强制转换,这些函数也将静默接受几种非字符串数据类型的值。但因为这样经常引起令人惊讶的行为,现在这些强制转换已被去掉。 但是,字符串连接运算符(||)仍然接受非字符串输入,只要输入中至少一个为字符串类型即可,如表9.8所示。 对于其他情况,如果您需要复制以前的行为,请在文本中进行显式强制转换。
Additional string manipulation functions are available and are listed in Table 9.9. Some of them are used internally to implement the SQL-standard string functions listed in Table 9.8.
其他可用字符串操作函数,在表9.9中列出。 其中一些用于内部实现SQL标准的字符串函数在表9.8中列出。
The concat, concat_ws and format functions are variadic, so it is possible to pass the values to be concatenated or formatted as an array marked with the VARIADIC keyword (see Section 38.5.5).The array's elements are treated as if they were separate ordinary arguments to the function. If the variadic array argument is NULL, concat and concat_ws return NULL, but format treats a NULL as a zero-element array.
concat,concat_ws和format函数是可变参数,因此可以将要串联或格式化的值传递为标有VARIADIC关键字的数组(请参见38.5.5节)。该函数将数组的元素视为独立的普通元素。如果可变参数数组参数为NULL,则concat和concat_ws返回NULL,但是format将NULL视为零元素数组。
See also the aggregate function string_agg in Section 9.20.
另请参见第9.20节中的聚合函数string_agg。
a The conversion names follow a standard naming scheme: The official name of the source encoding with all non-alphanumeric characters replaced by underscores, followed by _to_, followed by the similarly processed destination encoding name. Therefore, the names might deviate from the customary encoding names.
转换名称遵循标准的命名方案:源编码的正式名称,所有非字母数字字符均用下划线代替,后跟_to_,然后是经过类似处理的目标编码名称。 因此,名称可能与习惯的编码名称有所不同。