|
| 1 | +--- |
| 2 | +description: Reference for the 'substring' DSC configuration document function |
| 3 | +ms.date: 09/27/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: substring |
| 6 | +--- |
| 7 | + |
| 8 | +# substring |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Returns a substring that starts at the specified character position and contains |
| 13 | +the specified number of characters. |
| 14 | + |
| 15 | +## Syntax |
| 16 | + |
| 17 | +```Syntax |
| 18 | +substring(<stringToParse>, <startIndex>) |
| 19 | +substring(<stringToParse>, <startIndex>, <length>) |
| 20 | +``` |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +The `substring()` function extracts a portion of a string based on the specified |
| 25 | +starting position and optional length. The function uses zero-based indexing, |
| 26 | +meaning the first character is at position 0. This is useful for parsing |
| 27 | +identifiers, extracting prefixes or suffixes, manipulating configuration values, |
| 28 | +or formatting display strings. |
| 29 | + |
| 30 | +Key behaviors: |
| 31 | + |
| 32 | +- **Zero-based indexing**: The first character is at index 0 |
| 33 | +- **Optional length**: If length is omitted, returns the remainder of the string |
| 34 | + from the start position |
| 35 | +- **Boundary validation**: Prevents access beyond string boundaries with clear |
| 36 | + error messages |
| 37 | + |
| 38 | +## Examples |
| 39 | + |
| 40 | +### Example 1 - Extract environment from resource name |
| 41 | + |
| 42 | +This example demonstrates extracting environment information from standardized |
| 43 | +resource names for conditional configuration. It uses the [`parameters()`][08] |
| 44 | +function to retrieve the resource name. |
| 45 | + |
| 46 | +```yaml |
| 47 | +# substring.example.1.dsc.config.yaml |
| 48 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 49 | +parameters: |
| 50 | + resourceName: |
| 51 | + type: string |
| 52 | + defaultValue: svc-api-prod-east |
| 53 | +resources: |
| 54 | +- name: Extract environment |
| 55 | + type: Microsoft.DSC.Debug/Echo |
| 56 | + properties: |
| 57 | + output: |
| 58 | + environment: "[substring(parameters('resourceName'), 8, 4)]" |
| 59 | +``` |
| 60 | +
|
| 61 | +```bash |
| 62 | +dsc config get --file substring.example.1.dsc.config.yaml |
| 63 | +``` |
| 64 | + |
| 65 | +```yaml |
| 66 | +results: |
| 67 | +- name: Extract environment |
| 68 | + type: Microsoft.DSC.Debug/Echo |
| 69 | + result: |
| 70 | + actualState: |
| 71 | + output: |
| 72 | + environment: prod |
| 73 | +messages: [] |
| 74 | +hadErrors: false |
| 75 | +``` |
| 76 | +
|
| 77 | +### Example 2 - Extract region from resource identifier |
| 78 | +
|
| 79 | +This example shows extracting a region code from a standardized resource |
| 80 | +identifier without specifying length, using [`parameters()`][08] to retrieve |
| 81 | +the identifier. |
| 82 | + |
| 83 | +```yaml |
| 84 | +# substring.example.2.dsc.config.yaml |
| 85 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 86 | +parameters: |
| 87 | + resourceId: |
| 88 | + type: string |
| 89 | + defaultValue: app-web-eastus2-001 |
| 90 | +resources: |
| 91 | +- name: Extract region |
| 92 | + type: Microsoft.DSC.Debug/Echo |
| 93 | + properties: |
| 94 | + output: |
| 95 | + region: "[substring(parameters('resourceId'), 8)]" |
| 96 | +``` |
| 97 | + |
| 98 | +```bash |
| 99 | +dsc config get --file substring.example.2.dsc.config.yaml |
| 100 | +``` |
| 101 | + |
| 102 | +```yaml |
| 103 | +results: |
| 104 | +- name: Extract region |
| 105 | + type: Microsoft.DSC.Debug/Echo |
| 106 | + result: |
| 107 | + actualState: |
| 108 | + output: |
| 109 | + region: eastus2-001 |
| 110 | +messages: [] |
| 111 | +hadErrors: false |
| 112 | +``` |
| 113 | + |
| 114 | +### Example 3 - Parse version components |
| 115 | + |
| 116 | +This example demonstrates parsing semantic version strings to extract major, |
| 117 | +minor, and patch components. It uses [`parameters()`][08] to retrieve the |
| 118 | +version string. |
| 119 | + |
| 120 | +```yaml |
| 121 | +# substring.example.3.dsc.config.yaml |
| 122 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 123 | +parameters: |
| 124 | + version: |
| 125 | + type: string |
| 126 | + defaultValue: "3.2.1" |
| 127 | +resources: |
| 128 | +- name: Parse version |
| 129 | + type: Microsoft.DSC.Debug/Echo |
| 130 | + properties: |
| 131 | + output: |
| 132 | + major: "[substring(parameters('version'), 0, 1)]" |
| 133 | + minor: "[substring(parameters('version'), 2, 1)]" |
| 134 | + patch: "[substring(parameters('version'), 4, 1)]" |
| 135 | +``` |
| 136 | + |
| 137 | +```bash |
| 138 | +dsc config get --file substring.example.3.dsc.config.yaml |
| 139 | +``` |
| 140 | + |
| 141 | +```yaml |
| 142 | +results: |
| 143 | +- name: Parse version |
| 144 | + type: Microsoft.DSC.Debug/Echo |
| 145 | + result: |
| 146 | + actualState: |
| 147 | + output: |
| 148 | + major: "3" |
| 149 | + minor: "2" |
| 150 | + patch: "1" |
| 151 | +messages: [] |
| 152 | +hadErrors: false |
| 153 | +``` |
| 154 | + |
| 155 | +### Example 4 - Unicode and emoji support |
| 156 | + |
| 157 | +This example shows that `substring()` correctly handles Unicode characters and |
| 158 | +emojis. It uses [`parameters()`][08] to retrieve the message string. |
| 159 | + |
| 160 | +```yaml |
| 161 | +# substring.example.4.dsc.config.yaml |
| 162 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 163 | +parameters: |
| 164 | + message: |
| 165 | + type: string |
| 166 | + defaultValue: "Hello 🌍 World!" |
| 167 | +resources: |
| 168 | +- name: Unicode substring |
| 169 | + type: Microsoft.DSC.Debug/Echo |
| 170 | + properties: |
| 171 | + output: |
| 172 | + greeting: "[substring(parameters('message'), 0, 5)]" |
| 173 | + emoji: "[substring(parameters('message'), 6, 1)]" |
| 174 | + remainder: "[substring(parameters('message'), 8)]" |
| 175 | +``` |
| 176 | + |
| 177 | +```bash |
| 178 | +dsc config get --file substring.example.4.dsc.config.yaml |
| 179 | +``` |
| 180 | + |
| 181 | +```yaml |
| 182 | +results: |
| 183 | +- name: Unicode substring |
| 184 | + type: Microsoft.DSC.Debug/Echo |
| 185 | + result: |
| 186 | + actualState: |
| 187 | + output: |
| 188 | + greeting: Hello |
| 189 | + emoji: 🌍 |
| 190 | + remainder: " World!" |
| 191 | +messages: [] |
| 192 | +hadErrors: false |
| 193 | +``` |
| 194 | + |
| 195 | +## Parameters |
| 196 | + |
| 197 | +### stringToParse |
| 198 | + |
| 199 | +The original string from which the substring is extracted. |
| 200 | + |
| 201 | +```yaml |
| 202 | +Type: string |
| 203 | +Required: true |
| 204 | +Position: 1 |
| 205 | +``` |
| 206 | + |
| 207 | +### startIndex |
| 208 | + |
| 209 | +The zero-based starting character position for the substring. Must be a |
| 210 | +non-negative integer and cannot exceed the length of the string. |
| 211 | + |
| 212 | +```yaml |
| 213 | +Type: int |
| 214 | +Required: true |
| 215 | +Position: 2 |
| 216 | +``` |
| 217 | + |
| 218 | +### length |
| 219 | + |
| 220 | +The number of characters for the substring. Must be a non-negative integer. The |
| 221 | +start index plus length cannot exceed the length of the string. If omitted, the |
| 222 | +remainder of the string from the start position is returned. |
| 223 | + |
| 224 | +```yaml |
| 225 | +Type: int |
| 226 | +Required: false |
| 227 | +Position: 3 |
| 228 | +``` |
| 229 | + |
| 230 | +## Output |
| 231 | + |
| 232 | +The `substring()` function returns a string containing the extracted portion of |
| 233 | +the original string. |
| 234 | + |
| 235 | +```yaml |
| 236 | +Type: string |
| 237 | +``` |
| 238 | + |
| 239 | +## Exceptions |
| 240 | + |
| 241 | +The `substring()` function raises errors for the following conditions: |
| 242 | + |
| 243 | +- **Invalid start index**: When `startIndex` is negative |
| 244 | +- **Start index out of bounds**: When `startIndex` exceeds the string length |
| 245 | +- **Invalid length**: When `length` is negative |
| 246 | +- **Length out of bounds**: When `startIndex + length` exceeds the string |
| 247 | + length |
| 248 | + |
| 249 | +## Related functions |
| 250 | + |
| 251 | +- [`string()`][00] - Converts values to strings |
| 252 | +- [`concat()`][01] - Concatenates strings together |
| 253 | +- [`indexOf()`][02] - Finds the index of a substring in a string |
| 254 | +- [`lastIndexOf()`][03] - Finds the last index of a substring in a string |
| 255 | +- [`length()`][04] - Returns the length of a string or array |
| 256 | +- [`startsWith()`][05] - Checks if a string starts with a prefix |
| 257 | +- [`endsWith()`][06] - Checks if a string ends with a suffix |
| 258 | + |
| 259 | +<!-- Link reference definitions --> |
| 260 | +[00]: ./string.md |
| 261 | +[01]: ./concat.md |
| 262 | +[02]: ./indexOf.md |
| 263 | +[03]: ./lastIndexOf.md |
| 264 | +[04]: ./length.md |
| 265 | +[05]: ./startsWith.md |
| 266 | +[06]: ./endsWith.md |
| 267 | +[08]: ./parameters.md |
0 commit comments