strcmp

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

The strcmp function compares two strings lexicographically and returns an integer indicating their relationship. Use this function to sort strings, validate string ordering, or implement custom comparison logic in your queries.

Usage

Syntax

strcmp(string1, string2)

Parameters

NameTypeRequiredDescription
string1stringYesThe first string to compare.
string2stringYesThe second string to compare.

Returns

Returns an integer: -1 if string1 is less than string2, 0 if they're equal, 1 if string1 is greater than string2.

Use case examples

Compare HTTP methods to establish custom ordering for request type analysis.

Query

['sample-http-logs']
| extend method_order = strcmp(method, 'GET')
| summarize get_requests = countif(method_order == 0),
            before_get = countif(method_order < 0),
            after_get = countif(method_order > 0) by status
| limit 10

Run in Playground

Output

statusget_requestsbefore_getafter_get
200543212342109
4041987234120

This query uses strcmp to categorize HTTP methods relative to 'GET', enabling analysis of request type distribution by status code.

Compare service names to establish ordering for service dependency analysis.

Query

['otel-demo-traces']
| extend name_comparison = strcmp(['service.name'], 'frontend')
| extend is_frontend = name_comparison == 0
| extend before_frontend = name_comparison < 0
| extend after_frontend = name_comparison > 0
| summarize span_count = count() by is_frontend, before_frontend, after_frontend

Run in Playground

Output

is_frontendbefore_frontendafter_frontendspan_count
truefalsefalse4532
falsetruefalse3421
falsefalsetrue6012

This query categorizes services based on their lexicographic position relative to 'frontend', helping organize service hierarchies.

  • tolower: Converts strings to lowercase. Use this before strcmp for case-insensitive comparison.
  • toupper: Converts strings to uppercase. Use this before strcmp for case-insensitive comparison.
  • strlen: Returns string length. Use this to compare strings by length rather than lexicographically.
  • indexof: Finds substring positions. Use this for substring comparison rather than full string comparison.

Other query languages