Debian Codegraph @

doc/source/reference/functions/string_truncate.md

raw symbols SCIP
1 <!-- groonga-command -->
2 <!-- database: functions_string_truncate -->
3
4 # `string_truncate`
5
6 ```{versionadded} 16.0.9
7
8 ```
9
10 ## Summary
11
12 `string_truncate` truncates a string to at most the specified number of characters.
13 If the string is truncated, the tail of the kept characters is replaced with an omission mark so that the result,
14 including the omission mark, is `length` characters long, in the same way as Ruby on Rails' `String#truncate`.
15
16 To enable this function, register `functions/string` plugin with the following command:
17
18 ```
19 plugin_register functions/string
20 ```
21
22 ## Syntax
23
24 `string_truncate` requires two or three parameters.
25
26 ```
27 string_truncate(target, length[, options])
28 ```
29
30 `options` uses the following format. All key-value pairs are optional:
31
32 ```
33 {
34 "omission": omission
35 }
36 ```
37
38 ## Usage
39
40 Here are a schema definition and sample data to show usage.
41
42 Sample schema:
43
44 <!-- groonga-command -->
45
46 ```{include} ../../example/reference/functions/string_truncate/usage_setup_schema.md
47 plugin_register functions/string
48 table_create Memos TABLE_HASH_KEY ShortText
49 ```
50
51 Sample data:
52
53 <!-- groonga-command -->
54
55 ```{include} ../../example/reference/functions/string_truncate/usage_setup_data.md
56 load --table Memos
57 [
58 {"_key": "Groonga is a full text search engine"}
59 ]
60 ```
61
62 Here is a simple example.
63 Because the string is longer than the specified `length`, it's truncated and `"..."` is appended so that the result is 15 characters long.
64
65 <!-- groonga-command -->
66
67 ```{include} ../../example/reference/functions/string_truncate/usage_basic.md
68 select Memos \
69 --output_columns '_key, string_truncate(_key, 15)'
70 ```
71
72 If the string isn't longer than `length`, it's returned as-is without an omission mark.
73
74 <!-- groonga-command -->
75
76 ```{include} ../../example/reference/functions/string_truncate/usage_no_truncation.md
77 select Memos \
78 --output_columns '_key, string_truncate(_key, 100)'
79 ```
80
81 The following example specifies a custom `omission` in `options`.
82
83 <!-- groonga-command -->
84
85 ```{include} ../../example/reference/functions/string_truncate/usage_omission.md
86 select Memos \
87 --output_columns '_key, string_truncate(_key, 15, { "omission" : "***" })'
88 ```
89
90 When truncation occurs and `length` is smaller than the number of characters in `omission`, the result is only `omission` even if `omission` is longer than `length`.
91
92 <!-- groonga-command -->
93
94 ```{include} ../../example/reference/functions/string_truncate/usage_omission_only.md
95 select Memos \
96 --output_columns '_key, string_truncate(_key, 2)'
97 ```
98
99 You can specify a string literal instead of a column.
100
101 <!-- groonga-command -->
102
103 ```{include} ../../example/reference/functions/string_truncate/usage_string_literal.md
104 select Memos \
105 --output_columns 'string_truncate("Groonga is a fast fulltext search engine", 15)'
106 ```
107
108 ## Parameters
109
110 ### Required parameters
111
112 #### `target`
113
114 Specify a string literal or a string type column.
115
116 #### `length`
117
118 Specify the maximum number of characters of the result, including the `omission` mark when truncation occurs.
119 If the number of characters in `target` is less than or equal to `length`, `target` is returned as-is.
120
121 If `target` is longer than `length`, `string_truncate` subtracts the number of characters in `omission` from `length`, extracts that many characters from the beginning of `target`, and appends `omission`.
122 If `omission` is longer than `length` or `length` is negative, the result is `omission`.
123
124 ### Optional parameters
125
126 #### `options`
127
128 Specify the following key.
129
130 - `omission`
131 - Specify a string to be appended to the truncated string to show that it was truncated.
132 - The default is `"..."`.
133
134 ## Return value
135
136 `string_truncate` returns `target` truncated. When truncation occurs, the result (kept part + `omission`) is `length` characters long unless `omission` is longer than `length` or `length` is negative, in which case the result is `omission`.
137
138 ## See also
139
140 - {doc}`snippet`
141 - {doc}`string_substring`