Bicep functions - string - Azure Resource Manager (2023)

  • Article
  • 24 minutes to read

This article describes the Bicep functions for working with strings.

base64

base64(inputString)

Returns the base64 representation of the input string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
inputStringYesstringThe value to return as a base64 representation.

Return value

A string containing the base64 representation.

Examples

The following example shows how to use the base64 function.

param stringData string = 'one, two, three'param jsonFormattedData string = '{\'one\': \'a\', \'two\': \'b\'}'var base64String = base64(stringData)var base64Object = base64(jsonFormattedData)output base64Output string = base64Stringoutput toStringOutput string = base64ToString(base64String)output toJsonOutput object = base64ToJson(base64Object)

The output from the preceding example with the default values is:

NameTypeValue
base64OutputStringb25lLCB0d28sIHRocmVl
toStringOutputStringone, two, three
toJsonOutputObject{"one": "a", "two": "b"}

base64ToJson

base64ToJson(base64Value)

Converts a base64 representation to a JSON object.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
base64ValueYesstringThe base64 representation to convert to a JSON object.

Return value

A JSON object.

Examples

The following example uses the base64ToJson function to convert a base64 value:

param stringData string = 'one, two, three'param jsonFormattedData string = '{\'one\': \'a\', \'two\': \'b\'}'var base64String = base64(stringData)var base64Object = base64(jsonFormattedData)output base64Output string = base64Stringoutput toStringOutput string = base64ToString(base64String)output toJsonOutput object = base64ToJson(base64Object)

The output from the preceding example with the default values is:

NameTypeValue
base64OutputStringb25lLCB0d28sIHRocmVl
toStringOutputStringone, two, three
toJsonOutputObject{"one": "a", "two": "b"}

base64ToString

base64ToString(base64Value)

Converts a base64 representation to a string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
base64ValueYesstringThe base64 representation to convert to a string.

Return value

A string of the converted base64 value.

Examples

The following example uses the base64ToString function to convert a base64 value:

param stringData string = 'one, two, three'param jsonFormattedData string = '{\'one\': \'a\', \'two\': \'b\'}'var base64String = base64(stringData)var base64Object = base64(jsonFormattedData)output base64Output string = base64Stringoutput toStringOutput string = base64ToString(base64String)output toJsonOutput object = base64ToJson(base64Object)

The output from the preceding example with the default values is:

NameTypeValue
base64OutputStringb25lLCB0d28sIHRocmVl
toStringOutputStringone, two, three
toJsonOutputObject{"one": "a", "two": "b"}

concat

Instead of using the concat function, use string interpolation.

param prefix string = 'prefix'output concatOutput string = '${prefix}And${uniqueString(resourceGroup().id)}'

The output from the preceding example with the default values is:

NameTypeValue
concatOutputStringprefixAnd5yj4yjf5mbg72

Namespace: sys.

contains

contains(container, itemToFind)

Checks whether an array contains a value, an object contains a key, or a string contains a substring. The string comparison is case-sensitive. However, when testing if an object contains a key, the comparison is case-insensitive.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
containerYesarray, object, or stringThe value that contains the value to find.
itemToFindYesstring or intThe value to find.

Return value

True if the item is found; otherwise, False.

Examples

The following example shows how to use contains with different types:

param stringToTest string = 'OneTwoThree'param objectToTest object = { one: 'a' two: 'b' three: 'c'}param arrayToTest array = [ 'one' 'two' 'three']output stringTrue bool = contains(stringToTest, 'e')output stringFalse bool = contains(stringToTest, 'z')output objectTrue bool = contains(objectToTest, 'one')output objectFalse bool = contains(objectToTest, 'a')output arrayTrue bool = contains(arrayToTest, 'three')output arrayFalse bool = contains(arrayToTest, 'four')

The output from the preceding example with the default values is:

NameTypeValue
stringTrueBoolTrue
stringFalseBoolFalse
objectTrueBoolTrue
objectFalseBoolFalse
arrayTrueBoolTrue
arrayFalseBoolFalse

dataUri

dataUri(stringToConvert)

Converts a value to a data URI.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToConvertYesstringThe value to convert to a data URI.

Return value

A string formatted as a data URI.

Examples

The following example converts a value to a data URI, and converts a data URI to a string:

param stringToTest string = 'Hello'param dataFormattedString string = 'data:;base64,SGVsbG8sIFdvcmxkIQ=='output dataUriOutput string = dataUri(stringToTest)output toStringOutput string = dataUriToString(dataFormattedString)

The output from the preceding example with the default values is:

NameTypeValue
dataUriOutputStringdata:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutputStringHello, World!

dataUriToString

dataUriToString(dataUriToConvert)

Converts a data URI formatted value to a string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
dataUriToConvertYesstringThe data URI value to convert.

Return value

A string containing the converted value.

Examples

The following example converts a value to a data URI, and converts a data URI to a string:

param stringToTest string = 'Hello'param dataFormattedString string = 'data:;base64,SGVsbG8sIFdvcmxkIQ=='output dataUriOutput string = dataUri(stringToTest)output toStringOutput string = dataUriToString(dataFormattedString)

The output from the preceding example with the default values is:

NameTypeValue
dataUriOutputStringdata:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutputStringHello, World!

empty

empty(itemToTest)

Determines if an array, object, or string is empty.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
itemToTestYesarray, object, or stringThe value to check if it's empty.

Return value

Returns True if the value is empty; otherwise, False.

Examples

The following example checks whether an array, object, and string are empty.

param testArray array = []param testObject object = {}param testString string = ''output arrayEmpty bool = empty(testArray)output objectEmpty bool = empty(testObject)output stringEmpty bool = empty(testString)

The output from the preceding example with the default values is:

NameTypeValue
arrayEmptyBoolTrue
objectEmptyBoolTrue
stringEmptyBoolTrue

endsWith

endsWith(stringToSearch, stringToFind)

Determines whether a string ends with a value. The comparison is case-insensitive.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToSearchYesstringThe value that contains the item to find.
stringToFindYesstringThe value to find.

Return value

True if the last character or characters of the string match the value; otherwise, False.

Examples

The following example shows how to use the startsWith and endsWith functions:

output startsTrue bool = startsWith('abcdef', 'ab')output startsCapTrue bool = startsWith('abcdef', 'A')output startsFalse bool = startsWith('abcdef', 'e')output endsTrue bool = endsWith('abcdef', 'ef')output endsCapTrue bool = endsWith('abcdef', 'F')output endsFalse bool = endsWith('abcdef', 'e')

The output from the preceding example with the default values is:

NameTypeValue
startsTrueBoolTrue
startsCapTrueBoolTrue
startsFalseBoolFalse
endsTrueBoolTrue
endsCapTrueBoolTrue
endsFalseBoolFalse

first

first(arg1)

Returns the first character of the string, or first element of the array.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
arg1Yesarray or stringThe value to retrieve the first element or character.

Return value

A string of the first character, or the type (string, int, array, or object) of the first element in an array.

Examples

The following example shows how to use the first function with an array and string.

param arrayToTest array = [ 'one' 'two' 'three']output arrayOutput string = first(arrayToTest)output stringOutput string = first('One Two Three')

The output from the preceding example with the default values is:

NameTypeValue
arrayOutputStringone
stringOutputStringO

format

format(formatString, arg1, arg2, ...)

Creates a formatted string from input values.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
formatStringYesstringThe composite format string.
arg1Yesstring, integer, or booleanThe value to include in the formatted string.
additional argumentsNostring, integer, or booleanAdditional values to include in the formatted string.

Remarks

Use this function to format a string in your Bicep file. It uses the same formatting options as the System.String.Format method in .NET.

Examples

The following example shows how to use the format function.

param greeting string = 'Hello'param name string = 'User'param numberToFormat int = 8175133output formatTest string = format('{0}, {1}. Formatted number: {2:N0}', greeting, name, numberToFormat)

The output from the preceding example with the default values is:

NameTypeValue
formatTestStringHello, User. Formatted number: 8,175,133

guid

guid(baseString, ...)

Creates a value in the format of a globally unique identifier based on the values provided as parameters.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
baseStringYesstringThe value used in the hash function to create the GUID.
additional parameters as neededNostringYou can add as many strings as needed to create the value that specifies the level of uniqueness.

Remarks

This function is helpful when you need to create a value in the format of a globally unique identifier. You provide parameter values that limit the scope of uniqueness for the result. You can specify whether the name is unique down to subscription, resource group, or deployment.

The returned value isn't a random string, but rather the result of a hash function on the parameters. The returned value is 36 characters long. It isn't globally unique. To create a new GUID that isn't based on that hash value of the parameters, use the newGuid function.

Note

The order of the parameters affects the returned value. For example:

guid('hello', 'world') and guid('world', 'hello')

don't return the same value.

The following examples show how to use guid to create a unique value for commonly used levels.

Unique scoped to subscription

guid(subscription().subscriptionId)

Unique scoped to resource group

guid(resourceGroup().id)

Unique scoped to deployment for a resource group

guid(resourceGroup().id, deployment().name)

Return value

A string containing 36 characters in the format of a globally unique identifier.

Examples

The following example returns results from guid:

output guidPerSubscription string = guid(subscription().subscriptionId)output guidPerResourceGroup string = guid(resourceGroup().id)output guidPerDeployment string = guid(resourceGroup().id, deployment().name)

indexOf

indexOf(stringToSearch, stringToFind)

Returns the first position of a value within a string. The comparison is case-insensitive.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToSearchYesstringThe value that contains the item to find.
stringToFindYesstringThe value to find.

Return value

An integer that represents the position of the item to find. The value is zero-based. If the item isn't found, -1 is returned.

Examples

The following example shows how to use the indexOf and lastIndexOf functions:

output firstT int = indexOf('test', 't')output lastT int = lastIndexOf('test', 't')output firstString int = indexOf('abcdef', 'CD')output lastString int = lastIndexOf('abcdef', 'AB')output notFound int = indexOf('abcdef', 'z')

The output from the preceding example with the default values is:

NameTypeValue
firstTInt0
lastTInt3
firstStringInt2
lastStringInt0
notFoundInt-1

join

join(inputArray, delimiter)

Joins a string array into a single string, separated using a delimiter.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
inputArrayYesAn array of string.An array of strings to join.
delimiterYesThe delimiter to use for splitting the string.

Return value

A string.

Examples

The following example joins the input string array into strings delimited by either a comma or a semi-colon.

var arrayString = [ 'one' 'two' 'three']output firstOutput string = join(arrayString, ',')output secondOutput string = join(arrayString, ';')

The output from the preceding example with the default values is:

NameTypeValue
firstOutputString"one,two,three"
secondOutputString"one;two;three"

This function requires Bicep version 0.8.2 or later.

json

json(arg1)

Converts a valid JSON string into a JSON data type. For more information, see json function.

Namespace: sys.

last

last(arg1)

Returns last character of the string, or the last element of the array.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
arg1Yesarray or stringThe value to retrieve the last element or character.

Return value

A string of the last character, or the type (string, int, array, or object) of the last element in an array.

Examples

The following example shows how to use the last function with an array and string.

param arrayToTest array = [ 'one' 'two' 'three']output arrayOutput string = last(arrayToTest)output stringOutput string = last('One Two Three')

The output from the preceding example with the default values is:

NameTypeValue
arrayOutputStringthree
stringOutputStringe

lastIndexOf

lastIndexOf(stringToSearch, stringToFind)

Returns the last position of a value within a string. The comparison is case-insensitive.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToSearchYesstringThe value that contains the item to find.
stringToFindYesstringThe value to find.

Return value

An integer that represents the last position of the item to find. The value is zero-based. If the item isn't found, -1 is returned.

Examples

The following example shows how to use the indexOf and lastIndexOf functions:

output firstT int = indexOf('test', 't')output lastT int = lastIndexOf('test', 't')output firstString int = indexOf('abcdef', 'CD')output lastString int = lastIndexOf('abcdef', 'AB')output notFound int = indexOf('abcdef', 'z')

The output from the preceding example with the default values is:

NameTypeValue
firstTInt0
lastTInt3
firstStringInt2
lastStringInt0
notFoundInt-1

length

length(string)

Returns the number of characters in a string, elements in an array, or root-level properties in an object.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
arg1Yesarray, string, or objectThe array to use for getting the number of elements, the string to use for getting the number of characters, or the object to use for getting the number of root-level properties.

Return value

An int.

Examples

The following example shows how to use length with an array and string:

param arrayToTest array = [ 'one' 'two' 'three']param stringToTest string = 'One Two Three'param objectToTest object = { propA: 'one' propB: 'two' propC: 'three' propD: { 'propD-1': 'sub' 'propD-2': 'sub' }}output arrayLength int = length(arrayToTest)output stringLength int = length(stringToTest)output objectLength int = length(objectToTest)

The output from the preceding example with the default values is:

NameTypeValue
arrayLengthInt3
stringLengthInt13
objectLengthInt4

newGuid

newGuid()

Returns a value in the format of a globally unique identifier. This function can only be used in the default value for a parameter.

Namespace: sys.

Remarks

You can only use this function within an expression for the default value of a parameter. Using this function anywhere else in a Bicep file returns an error. The function isn't allowed in other parts of the Bicep file because it returns a different value each time it's called. Deploying the same Bicep file with the same parameters wouldn't reliably produce the same results.

The newGuid function differs from the guid function because it doesn't take any parameters. When you call guid with the same parameters, it returns the same identifier each time. Use guid when you need to reliably generate the same GUID for a specific environment. Use newGuid when you need a different identifier each time, such as deploying resources to a test environment.

The newGuid function uses the Guid structure in the .NET Framework to generate the globally unique identifier.

If you use the option to redeploy an earlier successful deployment, and the earlier deployment includes a parameter that uses newGuid, the parameter isn't reevaluated. Instead, the parameter value from the earlier deployment is automatically reused in the rollback deployment.

In a test environment, you may need to repeatedly deploy resources that only live for a short time. Rather than constructing unique names, you can use newGuid with uniqueString to create unique names.

Be careful redeploying a Bicep file that relies on the newGuid function for a default value. When you redeploy and don't provide a value for the parameter, the function is reevaluated. If you want to update an existing resource rather than create a new one, pass in the parameter value from the earlier deployment.

Return value

A string containing 36 characters in the format of a globally unique identifier.

Examples

The following example shows a parameter with a new identifier.

param guidValue string = newGuid()output guidOutput string = guidValue

The output from the preceding example varies for each deployment but will be similar to:

NameTypeValue
guidOutputstringb76a51fc-bd72-4a77-b9a2-3c29e7d2e551

The following example uses the newGuid function to create a unique name for a storage account. This Bicep file might work for test environment where the storage account exists for a short time and isn't redeployed.

param guidValue string = newGuid()var storageName = 'storage${uniqueString(guidValue)}'resource myStorage 'Microsoft.Storage/storageAccounts@2018-07-01' = { name: storageName location: 'West US' sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: {}}output nameOutput string = storageName

The output from the preceding example varies for each deployment but will be similar to:

NameTypeValue
nameOutputstringstoragenziwvyru7uxie

padLeft

padLeft(valueToPad, totalLength, paddingCharacter)

Returns a right-aligned string by adding characters to the left until reaching the total specified length.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
valueToPadYesstring or intThe value to right-align.
totalLengthYesintThe total number of characters in the returned string.
paddingCharacterNosingle characterThe character to use for left-padding until the total length is reached. The default value is a space.

If the original string is longer than the number of characters to pad, no characters are added.

Return value

A string with at least the number of specified characters.

Examples

The following example shows how to pad the user-provided parameter value by adding the zero character until it reaches the total number of characters.

param testString string = '123'output stringOutput string = padLeft(testString, 10, '0')

The output from the preceding example with the default values is:

NameTypeValue
stringOutputString0000000123

replace

replace(originalString, oldString, newString)

Returns a new string with all instances of one string replaced by another string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
originalStringYesstringThe value that has all instances of one string replaced by another string.
oldStringYesstringThe string to be removed from the original string.
newStringYesstringThe string to add in place of the removed string.

Return value

A string with the replaced characters.

Examples

The following example shows how to remove all dashes from the user-provided string, and how to replace part of the string with another string.

param testString string = '123-123-1234'output firstOutput string = replace(testString, '-', '')output secondOutput string = replace(testString, '1234', 'xxxx')

The output from the preceding example with the default values is:

NameTypeValue
firstOutputString1231231234
secondOutputString123-123-xxxx

skip

skip(originalValue, numberToSkip)

Returns a string with all the characters after the specified number of characters, or an array with all the elements after the specified number of elements.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
originalValueYesarray or stringThe array or string to use for skipping.
numberToSkipYesintThe number of elements or characters to skip. If this value is 0 or less, all the elements or characters in the value are returned. If it's larger than the length of the array or string, an empty array or string is returned.

Return value

An array or string.

Examples

The following example skips the specified number of elements in the array, and the specified number of characters in a string.

param testArray array = [ 'one' 'two' 'three']param elementsToSkip int = 2param testString string = 'one two three'param charactersToSkip int = 4output arrayOutput array = skip(testArray, elementsToSkip)output stringOutput string = skip(testString, charactersToSkip)

The output from the preceding example with the default values is:

NameTypeValue
arrayOutputArray["three"]
stringOutputStringtwo three

split

split(inputString, delimiter)

Returns an array of strings that contains the substrings of the input string that are delimited by the specified delimiters.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
inputStringYesstringThe string to split.
delimiterYesstring or array of stringsThe delimiter to use for splitting the string.

Return value

An array of strings.

Examples

The following example splits the input string with a comma, and with either a comma or a semi-colon.

param firstString string = 'one,two,three'param secondString string = 'one;two,three'var delimiters = [ ',' ';']output firstOutput array = split(firstString, ',')output secondOutput array = split(secondString, delimiters)

The output from the preceding example with the default values is:

NameTypeValue
firstOutputArray["one", "two", "three"]
secondOutputArray["one", "two", "three"]

startsWith

startsWith(stringToSearch, stringToFind)

Determines whether a string starts with a value. The comparison is case-insensitive.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToSearchYesstringThe value that contains the item to find.
stringToFindYesstringThe value to find.

Return value

True if the first character or characters of the string match the value; otherwise, False.

Examples

The following example shows how to use the startsWith and endsWith functions:

output startsTrue bool = startsWith('abcdef', 'ab')output startsCapTrue bool = startsWith('abcdef', 'A')output startsFalse bool = startsWith('abcdef', 'e')output endsTrue bool = endsWith('abcdef', 'ef')output endsCapTrue bool = endsWith('abcdef', 'F')output endsFalse bool = endsWith('abcdef', 'e')

The output from the preceding example with the default values is:

NameTypeValue
startsTrueBoolTrue
startsCapTrueBoolTrue
startsFalseBoolFalse
endsTrueBoolTrue
endsCapTrueBoolTrue
endsFalseBoolFalse

string

string(valueToConvert)

Converts the specified value to a string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
valueToConvertYesAnyThe value to convert to string. Any type of value can be converted, including objects and arrays.

Return value

A string of the converted value.

Examples

The following example shows how to convert different types of values to strings:

param testObject object = { valueA: 10 valueB: 'Example Text'}param testArray array = [ 'a' 'b' 'c']param testInt int = 5output objectOutput string = string(testObject)output arrayOutput string = string(testArray)output intOutput string = string(testInt)

The output from the preceding example with the default values is:

NameTypeValue
objectOutputString{"valueA":10,"valueB":"Example Text"}
arrayOutputString["a","b","c"]
intOutputString5

substring

substring(stringToParse, startIndex, length)

Returns a substring that starts at the specified character position and contains the specified number of characters.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToParseYesstringThe original string from which the substring is extracted.
startIndexNointThe zero-based starting character position for the substring.
lengthNointThe number of characters for the substring. Must refer to a location within the string. Must be zero or greater. If omitted, the remainder of the string from the start position will be returned.

Return value

The substring. Or, an empty string if the length is zero.

Remarks

The function fails when the substring extends beyond the end of the string, or when length is less than zero. The following example fails with the error "The index and length parameters must refer to a location within the string. The index parameter: '0', the length parameter: '11', the length of the string parameter: '10'.".

param inputString string = '1234567890'var prefix = substring(inputString, 0, 11)

Examples

The following example extracts a substring from a parameter.

param testString string = 'one two three'output substringOutput string = substring(testString, 4, 3)

The output from the preceding example with the default values is:

NameTypeValue
substringOutputStringtwo

take

take(originalValue, numberToTake)

Returns a string with the specified number of characters from the start of the string, or an array with the specified number of elements from the start of the array.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
originalValueYesarray or stringThe array or string to take the elements from.
numberToTakeYesintThe number of elements or characters to take. If this value is 0 or less, an empty array or string is returned. If it's larger than the length of the given array or string, all the elements in the array or string are returned.

Return value

An array or string.

Examples

The following example takes the specified number of elements from the array, and characters from a string.

param testArray array = [ 'one' 'two' 'three']param elementsToTake int = 2param testString string = 'one two three'param charactersToTake int = 2output arrayOutput array = take(testArray, elementsToTake)output stringOutput string = take(testString, charactersToTake)

The output from the preceding example with the default values is:

NameTypeValue
arrayOutputArray["one", "two"]
stringOutputStringon

toLower

toLower(stringToChange)

Converts the specified string to lower case.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToChangeYesstringThe value to convert to lower case.

Return value

The string converted to lower case.

Examples

The following example converts a parameter value to lower case and to upper case.

param testString string = 'One Two Three'output toLowerOutput string = toLower(testString)output toUpperOutput string = toUpper(testString)

The output from the preceding example with the default values is:

NameTypeValue
toLowerOutputStringone two three
toUpperOutputStringONE TWO THREE

toUpper

toUpper(stringToChange)

Converts the specified string to upper case.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToChangeYesstringThe value to convert to upper case.

Return value

The string converted to upper case.

Examples

The following example converts a parameter value to lower case and to upper case.

param testString string = 'One Two Three'output toLowerOutput string = toLower(testString)output toUpperOutput string = toUpper(testString)

The output from the preceding example with the default values is:

NameTypeValue
toLowerOutputStringone two three
toUpperOutputStringONE TWO THREE

trim

trim(stringToTrim)

Removes all leading and trailing white-space characters from the specified string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToTrimYesstringThe value to trim.

Return value

The string without leading and trailing white-space characters.

Examples

The following example trims the white-space characters from the parameter.

param testString string = ' one two three 'output return string = trim(testString)

The output from the preceding example with the default values is:

NameTypeValue
returnStringone two three

uniqueString

uniqueString(baseString, ...)

Creates a deterministic hash string based on the values provided as parameters.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
baseStringYesstringThe value used in the hash function to create a unique string.
additional parameters as neededNostringYou can add as many strings as needed to create the value that specifies the level of uniqueness.

Remarks

This function is helpful when you need to create a unique name for a resource. You provide parameter values that limit the scope of uniqueness for the result. You can specify whether the name is unique down to subscription, resource group, or deployment.

The returned value isn't a random string, but rather the result of a hash function. The returned value is 13 characters long. It isn't globally unique. You may want to combine the value with a prefix from your naming convention to create a name that is meaningful. The following example shows the format of the returned value. The actual value varies by the provided parameters.

tcvhiyu5h2o5o

The following examples show how to use uniqueString to create a unique value for commonly used levels.

Unique scoped to subscription

uniqueString(subscription().subscriptionId)

Unique scoped to resource group

uniqueString(resourceGroup().id)

Unique scoped to deployment for a resource group

uniqueString(resourceGroup().id, deployment().name)

The following example shows how to create a unique name for a storage account based on your resource group. Inside the resource group, the name isn't unique if constructed the same way.

resource mystorage 'Microsoft.Storage/storageAccounts@2018-07-01' = { name: 'storage${uniqueString(resourceGroup().id)}' ...}

If you need to create a new unique name each time you deploy a Bicep file, and don't intend to update the resource, you can use the utcNow function with uniqueString. You could use this approach in a test environment. For an example, see utcNow. Note the utcNow function can only be used within an expression for the default value of a parameter.

Return value

A string containing 13 characters.

Examples

The following example returns results from uniquestring:

output uniqueRG string = uniqueString(resourceGroup().id)output uniqueDeploy string = uniqueString(resourceGroup().id, deployment().name)

uri

uri(baseUri, relativeUri)

Creates an absolute URI by combining the baseUri and the relativeUri string.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
baseUriYesstringThe base uri string. Take care to observe the behavior regarding the handling of the trailing slash ('/'), as described following this table.
relativeUriYesstringThe relative uri string to add to the base uri string.
  • If baseUri ends in a trailing slash, the result is simplybaseUri followed by relativeUri.

  • If baseUri does not end in a trailing slash one of two thingshappens.

    • If baseUri has no slashes at all (aside from the "//" nearthe front) the result is simply baseUri followed by relativeUri.

    • If baseUri has some slashes, but doesn't end with a slash,everything from the last slash onward is removed from baseUriand the result is baseUri followed by relativeUri.

Here are some examples:

uri('http://contoso.org/firstpath', 'myscript.sh') -> http://contoso.org/myscript.shuri('http://contoso.org/firstpath/', 'myscript.sh') -> http://contoso.org/firstpath/myscript.shuri('http://contoso.org/firstpath/azuredeploy.json', 'myscript.sh') -> http://contoso.org/firstpath/myscript.shuri('http://contoso.org/firstpath/azuredeploy.json/', 'myscript.sh') -> http://contoso.org/firstpath/azuredeploy.json/myscript.sh

For complete details, the baseUri and relativeUri parameters areresolved as specified inRFC 3986, section 5.

Return value

A string representing the absolute URI for the base and relative values.

Examples

The following example shows how to use uri, uriComponent, and uriComponentToString:

var uriFormat = uri('http://contoso.com/resources/', 'nested/azuredeploy.json')var uriEncoded = uriComponent(uriFormat)output uriOutput string = uriFormatoutput componentOutput string = uriEncodedoutput toStringOutput string = uriComponentToString(uriEncoded)

The output from the preceding example with the default values is:

NameTypeValue
uriOutputStringhttp://contoso.com/resources/nested/azuredeploy.json
componentOutputStringhttp%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputStringhttp://contoso.com/resources/nested/azuredeploy.json

uriComponent

uricomponent(stringToEncode)

Encodes a URI.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
stringToEncodeYesstringThe value to encode.

Return value

A string of the URI encoded value.

Examples

The following example shows how to use uri, uriComponent, and uriComponentToString:

var uriFormat = uri('http://contoso.com/resources/', 'nested/azuredeploy.json')var uriEncoded = uriComponent(uriFormat)output uriOutput string = uriFormatoutput componentOutput string = uriEncodedoutput toStringOutput string = uriComponentToString(uriEncoded)

The output from the preceding example with the default values is:

NameTypeValue
uriOutputStringhttp://contoso.com/resources/nested/azuredeploy.json
componentOutputStringhttp%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputStringhttp://contoso.com/resources/nested/azuredeploy.json

uriComponentToString

uriComponentToString(uriEncodedString)

Returns a string of a URI encoded value.

Namespace: sys.

Parameters

ParameterRequiredTypeDescription
uriEncodedStringYesstringThe URI encoded value to convert to a string.

Return value

A decoded string of URI encoded value.

Examples

The following example shows how to use uri, uriComponent, and uriComponentToString:

var uriFormat = uri('http://contoso.com/resources/', 'nested/azuredeploy.json')var uriEncoded = uriComponent(uriFormat)output uriOutput string = uriFormatoutput componentOutput string = uriEncodedoutput toStringOutput string = uriComponentToString(uriEncoded)

The output from the preceding example with the default values is:

NameTypeValue
uriOutputStringhttp://contoso.com/resources/nested/azuredeploy.json
componentOutputStringhttp%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputStringhttp://contoso.com/resources/nested/azuredeploy.json

Next steps

  • For a description of the sections in a Bicep file, see Understand the structure and syntax of Bicep files.
  • To iterate a specified number of times when creating a type of resource, see Iterative loops in Bicep.
  • To see how to deploy the Bicep file you've created, see Deploy resources with Bicep and Azure PowerShell.
Top Articles
Latest Posts
Article information

Author: Ray Christiansen

Last Updated: 02/18/2023

Views: 6285

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.