Skip to content

substr

Description

Extracts a substring from a string starting at a given position, with an optional length. Useful for simple value extraction without needing regular expressions.

Syntax

Like many functions in DataPrime, substr supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

substr(value: string, from: number, length?: number): string
(value: string).substr(from: number, length?: number): string

Arguments

NameTypeRequiredDescription
valuestringtrueThe string to extract from
fromnumbertrueThe starting index for the substring
lengthnumberfalseNumber of characters to return. Defaults to the remainder of the string

Example

Extract the value after a delimiter

Consider the following document:

{
    "msg": "result=Some value"
}

First, find the position of = with indexOf, then use substr to capture everything after it:

create index_of_value from (indexOf(msg, '=') + 1)
| create value from substr(msg, index_of_value)
create index_of_value from (msg.indexOf('=') + 1)
| create value from msg.substr(index_of_value)

Output

{
    "msg": "result=Some value",
    "index_of_value": 7,
    "value": "Some value"
}