To check if a string value starts or ends with a given string you can use the str starts-with and str ends-with commands.
The command returns a boolean value: true if the string starts or ends with the given string and false otherwise.
The commands are case sensitive by default.
You can use the --ignore-case (or the shorthand -i) to ignore casing while checking if a the string starts or ends with a given string.
To input can be a string value and then that string value is checked.
If the input is an array of string values, then each element is checked.
It is also possible to check values in a record or table.
You need to pass the names of the field(s) or column(s) that you want to check the string values of.
The next example shows several uses of the str starts-with command:
use std/assert
assert ("mrhaki" | str starts-with "mr")
assert not ("Hubert" | str starts-with "mr")
# Using --ignore-case to check with ignoring casing.
assert ("MrHaki" | str starts-with --ignore-case "mr")
# Using shorthand -i to check with ignoring casing.
assert ("MrHaki" | str starts-with -i "mr")
# With an array as input each element is tested to
# see if it starts with the given string.
assert equal (["mrhaki" "Hubert"] | str starts-with "mr") [true false]
# With a record as input you can give the name of
# the field(s) to check if the value is starting with
# a given string.
let data = {name: "mrhaki", shell: "NuShell"}
assert ($data | str starts-with "mr" name).name
assert not ($data | str starts-with "mr" shell).shell
(assert equal ($data | str starts-with "mr" name shell)
{name: true shell: false})
# With a table as input you can give the name of
# the field(s) to check if the value is starting with
# a given string.
let users = [[name shell]; ["mrhaki" "NuShell"] ["Hubert" "bash"]]
assert equal ($users | str starts-with "mr" name).name [true false]
assert equal ($users | str starts-with "mr" shell).shell [false false]
(assert equal ($users | str starts-with "mr" name shell)
[[name shell]; [true false] [false false]])
In the following example you can see several ways to use the str ends-with command:
use std/assert
assert ("Nushell is awesome!" | str ends-with "!")
assert not ("Nushell rocks" | str ends-with "!")
# Using --ignore-case to check with ignoring casing.
assert ("Nushell rocks" | str ends-with --ignore-case "Rocks")
# Using shorthand -i to check with ignoring casing.
assert ("Nushell rocks" | str ends-with -i "Rocks")
# With an array as input each element is tested to
# see if it ends with the given string.
(assert equal (["Nushell is awesome!" "Nushell rocks"] | str ends-with "!")
[true false])
# With a record as input you can give the name of
# the field(s) to check if the value is ending with
# a given string.
let data = {name: "Nushell", exclamation: "rocks!"}
assert ($data | str ends-with "!" exclamation).exclamation
assert not ($data | str ends-with "!" name).name
(assert equal ($data | str ends-with "!" name exclamation)
{name: false exclamation: true})
# With a table as input you can give the name of
# the field(s) to check if the value is starting with
# a given string.
let shells = [[name exclamation]; ["Nushell" "rocks!"] ["Nushell" "awesome"]]
assert equal ($shells | str ends-with "!" exclamation).exclamation [true false]
assert equal ($shells | str ends-with "!" name).name [false false]
(assert equal ($shells | str ends-with "!" name exclamation)
[[name exclamation]; [false true] [false false]])
Written with Nushell 0.109.0.