split() / array_length()

split(text, separator) cuts a string wherever the separator appears and returns the pieces as a list (a dynamic array):

print Parts = split("ws-prg-011.kestrelbay.example", ".")
// ["ws-prg-011","kestrelbay","example"]

Reading the parts

  • split(x, ".")[0] is the first piece, [1] the second, and [-1] the last.
  • An index past the end gives an empty value, not an error.
  • The pieces are dynamic values. Wrap them in tostring() before you compare, group or sort by them.

array_length()

array_length(list) returns the number of items in a list: from split(), from make_set(), or from a list in a dynamic column. It returns null when the value isn’t a list.

Things to know

  • The separator is plain text, not a regular expression. For pattern-based cutting, use extract().
  • If the separator does not occur, you get a list with one item, the whole string.
  • Two separators in a row give an empty piece between them: split("a,,b", ",") has three items.
  • split() also takes an optional third argument: the index of the one piece to return (still as a one-item list).

Examples

1 The name part of a user principal name
SigninLogs
| where Location == "GB"
| extend Name = tostring(split(UserPrincipalName, "@")[0])
| distinct Name

[0] is the first part. tostring() turns the dynamic value into a normal string.

2 How deep is each program's folder?
DeviceProcessEvents
| where FileName in ("OneDrive.exe", "MpCmdRun.exe", "explorer.exe")
| extend Folders = split(FolderPath, @"\")
| summarize Depth = max(array_length(Folders)) by FileName
3 The first label of each host name
DeviceNetworkEvents
| where isnotempty(RemoteUrl)
| extend Host = tostring(split(RemoteUrl, ".")[0])
| summarize Urls = dcount(RemoteUrl) by Host
| sort by Urls desc

Practice it