Skip to content

startsWith - Check if a string starts with a given prefix

The startsWith function returns true if a given substring appears at the start of a string, and false if it does not.

NOTE: This is different from contains, which will check if a substring appears anywhere within a given string.

Syntax

startsWith(string: string, prefix: string): bool

Arguments

NameTypeRequiredDescription
stringstringtrueThe full string to test i.e the haystack
prefixstringtrueThe prefix to check against the string i.e the needle

Example - Check if a string contains an IBM CRN

IBM Cloud CRNs look something like this: crn:v1:bluemix:public:cloud-object-storage:global:a/59bcbfa6ea2f006b4ed7094c1a08dcdd:1a0ec336-f391-4091-a6fb-5e084a4c56f4:bucket:mybucket

If we wish to check if a given string is an IBM CRN, we can check if it begins with 'crn:'.

create is_crn from startsWith(crn, 'crn:')

This will define a new boolean field indicating whether a given string is an IBM CRN.

Another way to write this query is to check it directly against the crn field:

create is_crn from crn.startsWith('crn:')

Try it yourself

Open up your log explore screen and run the following command:

create email from '[email protected]'
| create is_chris from email.startsWith('chris')
| choose email, is_chris