indexof

This page explains how to use the indexof function in APL.

The indexof function reports the zero-based index of the first occurrence of a specified string within an input string. Use this function to find the position of substrings, validate string formats, or extract parts of strings based on delimiter positions.

Usage

Syntax

indexof(source, lookup, start_index, length, occurrence)

Parameters

NameTypeRequiredDescription
sourcestringYesThe input string to search within.
lookupstringYesThe string to search for.
start_indexintNoThe position to start searching from (default: 0).
lengthintNoNumber of character positions to examine. Use -1 for unlimited (default: -1).
occurrenceintNoThe occurrence number to find (default: 1 for first occurrence).

Returns

Returns the zero-based index position of the first occurrence of the lookup string, or -1 if not found.

Use case examples

Find the position of API version indicators in URIs to categorize and analyze API usage patterns.

Query

['sample-http-logs']
| extend api_pos = indexof(uri, '/api/')
| where api_pos >= 0
| extend has_version = indexof(uri, '/v', api_pos)
| project _time, uri, api_pos, has_version, method, status
| limit 10

Run in Playground

Output

_timeuriapi_poshas_versionmethodstatus
2024-11-06T10:00:00Z/api/v2/users04GET200
2024-11-06T10:01:00Z/api/products0-1GET200
2024-11-06T10:02:00Z/api/v1/orders04POST201

This query finds the position of API indicators in URIs, helping identify versioned versus unversioned API endpoints.

Locate service name delimiters to extract service identifiers from composite names.

Query

['otel-demo-traces']
| extend dash_pos = indexof(['service.name'], '-')
| where dash_pos >= 0
| extend service_prefix = substring(['service.name'], 0, dash_pos)
| summarize span_count = count() by service_prefix
| sort by span_count desc
| limit 10

Run in Playground

Output

service_prefixspan_count
otel8765
service4321
app2345

This query uses indexof to find delimiter positions in service names, enabling extraction of service prefixes for grouping and analysis.

Detect SQL injection attempts by finding the position of SQL keywords in URIs.

Query

['sample-http-logs']
| extend union_pos = indexof(tolower(uri), 'union'),
         select_pos = indexof(tolower(uri), 'select'),
         drop_pos = indexof(tolower(uri), 'drop')
| where union_pos >= 0 or select_pos >= 0 or drop_pos >= 0
| project _time, uri, union_pos, select_pos, drop_pos, id, status, ['geo.country']
| sort by _time desc
| limit 10

Run in Playground

Output

_timeuriunion_posselect_posdrop_posidstatusgeo.country
2024-11-06T10:00:00Z/api?id=1'union select-111-1user123403Unknown
2024-11-06T10:01:00Z/search?q=drop table-1-110user456403Russia

This query identifies potential SQL injection attempts by finding the position of SQL keywords in URIs, helping security teams detect and respond to attacks.

  • substring: Extracts a substring from a source string. Use this together with indexof to extract parts of strings based on found positions.
  • strlen: Returns the length of a string. Use this with indexof to calculate positions relative to string length.
  • extract: Extracts substrings using regular expressions. Use this when you need pattern matching instead of simple substring positions.
  • split: Splits strings by delimiters. Use this when you want to tokenize rather than find positions.

Other query languages