--- type: "fhirpath-function" title: "FHIRPath Function: substring" function: "substring" category: "String Manipulation" section: "5.6.3" source: "fhirpath/functions.json" --- # FHIRPath Function: substring Returns the part of the string starting at position `start` (zero-based). If `length` is given, will return at most `length` number of characters from the input string. If `start` lies outside the length of the string, the function returns empty (`{ }`). If there are less remaining characters in the string than indicated by `length`, the function returns just the remaining characters. If the input or `start` is empty, the result is empty. If an empty `length` is provided, the behavior is the same as if `length` had not been provided. If a negative or zero `length` is provided, the function returns an empty string (`''`). If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment. ``` fhirpath 'abcdefg'.substring(3) // 'defg' 'abcdefg'.substring(1, 2) // 'bc' 'abcdefg'.substring(6, 2) // 'g' 'abcdefg'.substring(7, 1) // { } (start position is outside the string) 'abcdefg'.substring(-1, 1) // { } (start position is outside the string, // this can happen when the -1 was the result of a calculation rather than explicitly provided) 'abcdefg'.substring(3, 0) // '' (empty string) 'abcdefg'.substring(3, -1) // '' (empty string) 'abcdefg'.substring(-1, -1) // {} (start position is outside the string) ``` ## Summary - **Category**: String Manipulation - **Section**: `5.6.3` - **Return Type**: `String` - **Empty Input Result**: `empty` - **Errors on Multiple Input**: `true` ## Description Returns the part of the string starting at position `start` (zero-based). If `length` is given, will return at most `length` number of characters from the input string. ## Arguments - `start`: `Integer` - Start position (zero-based) - `length` (optional): `Integer` - Optional length of substring to return ## Type Mapping - `String-String` ## Example ```fhirpath 'abcdefg'.substring(3) // 'defg' 'abcdefg'.substring(1, 2) // 'bc' ```