ARM 模板的字符串函数String functions for ARM templates

资源管理器提供了以下用于处理 Azure 资源管理器 (ARM) 模板中字符串的函数:Resource Manager provides the following functions for working with strings in your Azure Resource Manager (ARM) templates:

base64base64

base64(inputString)

返回输入字符串的 base64 表示形式。Returns the base64 representation of the input string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
inputStringinputString Yes stringstring 要以 base64 表示形式返回的值。The value to return as a base64 representation.

返回值Return value

包含 base64 表示形式的字符串。A string containing the base64 representation.

示例Examples

以下示例模板演示如何使用 base64 函数。The following example template shows how to use the base64 function.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringData": {
            "type": "string",
            "defaultValue": "one, two, three"
        },
        "jsonFormattedData": {
            "type": "string",
            "defaultValue": "{'one': 'a', 'two': 'b'}"
        }
    },
    "variables": {
        "base64String": "[base64(parameters('stringData'))]",
        "base64Object": "[base64(parameters('jsonFormattedData'))]"
    },
    "resources": [
    ],
    "outputs": {
        "base64Output": {
            "type": "string",
            "value": "[variables('base64String')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[base64ToString(variables('base64String'))]"
        },
        "toJsonOutput": {
            "type": "object",
            "value": "[base64ToJson(variables('base64Object'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
base64Outputbase64Output StringString b25lLCB0d28sIHRocmVlb25lLCB0d28sIHRocmVl
toStringOutputtoStringOutput StringString one, two, threeone, two, three
toJsonOutputtoJsonOutput ObjectObject {"one": "a", "two": "b"}{"one": "a", "two": "b"}

base64ToJsonbase64ToJson

base64tojson

将 base64 表示形式转换为 JSON 对象。Converts a base64 representation to a JSON object.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
base64Valuebase64Value Yes stringstring 要转换为 JSON 对象的 base64 表示形式。The base64 representation to convert to a JSON object.

返回值Return value

一个 JSON 对象。A JSON object.

示例Examples

以下示例模板使用 base64ToJson 函数转换 base64 值:The following example template uses the base64ToJson function to convert a base64 value:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringData": {
            "type": "string",
            "defaultValue": "one, two, three"
        },
        "jsonFormattedData": {
            "type": "string",
            "defaultValue": "{'one': 'a', 'two': 'b'}"
        }
    },
    "variables": {
        "base64String": "[base64(parameters('stringData'))]",
        "base64Object": "[base64(parameters('jsonFormattedData'))]"
    },
    "resources": [
    ],
    "outputs": {
        "base64Output": {
            "type": "string",
            "value": "[variables('base64String')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[base64ToString(variables('base64String'))]"
        },
        "toJsonOutput": {
            "type": "object",
            "value": "[base64ToJson(variables('base64Object'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
base64Outputbase64Output StringString b25lLCB0d28sIHRocmVlb25lLCB0d28sIHRocmVl
toStringOutputtoStringOutput StringString one, two, threeone, two, three
toJsonOutputtoJsonOutput ObjectObject {"one": "a", "two": "b"}{"one": "a", "two": "b"}

base64ToStringbase64ToString

base64ToString(base64Value)

将 base64 表示形式转换为字符串。Converts a base64 representation to a string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
base64Valuebase64Value Yes stringstring 要转换为字符串的 base64 表示形式。The base64 representation to convert to a string.

返回值Return value

转换后的 base64 值的字符串。A string of the converted base64 value.

示例Examples

以下示例模板使用 base64ToString 函数转换 base64 值:The following example template uses the base64ToString function to convert a base64 value:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringData": {
            "type": "string",
            "defaultValue": "one, two, three"
        },
        "jsonFormattedData": {
            "type": "string",
            "defaultValue": "{'one': 'a', 'two': 'b'}"
        }
    },
    "variables": {
        "base64String": "[base64(parameters('stringData'))]",
        "base64Object": "[base64(parameters('jsonFormattedData'))]"
    },
    "resources": [
    ],
    "outputs": {
        "base64Output": {
            "type": "string",
            "value": "[variables('base64String')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[base64ToString(variables('base64String'))]"
        },
        "toJsonOutput": {
            "type": "object",
            "value": "[base64ToJson(variables('base64Object'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
base64Outputbase64Output StringString b25lLCB0d28sIHRocmVlb25lLCB0d28sIHRocmVl
toStringOutputtoStringOutput StringString one, two, threeone, two, three
toJsonOutputtoJsonOutput ObjectObject {"one": "a", "two": "b"}{"one": "a", "two": "b"}

concatconcat

concat (arg1, arg2, arg3, ...)

合并多个字符串值并返回串联的字符串,或合并多个数组并返回串联的数组。Combines multiple string values and returns the concatenated string, or combines multiple arrays and returns the concatenated array.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 字符串或数组string or array 要串联的第一个字符串或数组。The first string or array for concatenation.
其他参数additional arguments No 字符串或数组string or array 要串联的按顺序排列的其他字符串或数组。Additional strings or arrays in sequential order for concatenation.

此函数可以使用任意数量的参数,并可接受字符串或数组作为参数。This function can take any number of arguments, and can accept either strings or arrays for the parameters. 但是,不能同时为参数提供数组和字符串。However, you can't provide both arrays and strings for parameters. 字符串仅与其他字符串连接。Strings are only concatenated with other strings.

返回值Return value

由串联值构成的字符串或数组。A string or array of concatenated values.

示例Examples

以下示例模板演示如何组合两个字符串值并返回串联的字符串。The following example template shows how to combine two string values and return a concatenated string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "prefix": {
            "type": "string",
            "defaultValue": "prefix"
        }
    },
    "resources": [],
    "outputs": {
        "concatOutput": {
            "value": "[concat(parameters('prefix'), '-', uniqueString(resourceGroup().id))]",
            "type" : "string"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
concatOutputconcatOutput StringString prefix-5yj4yjf5mbg72prefix-5yj4yjf5mbg72

以下示例模板演示如何组合两个数组。The following example template shows how to combine two arrays.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "firstArray": {
            "type": "array",
            "defaultValue": [
                "1-1",
                "1-2",
                "1-3"
            ]
        },
        "secondArray": {
            "type": "array",
            "defaultValue": [
                "2-1",
                "2-2",
                "2-3"
            ]
        }
    },
    "resources": [
    ],
    "outputs": {
        "return": {
            "type": "array",
            "value": "[concat(parameters('firstArray'), parameters('secondArray'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
returnreturn ArrayArray ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

containscontains

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.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
containercontainer Yes 数组、对象或字符串array, object, or string 包含要查找的值的值。The value that contains the value to find.
itemToFinditemToFind Yes 字符串或整数string or int 要查找的值。The value to find.

返回值Return value

如果找到该项,则为 True;否则为 FalseTrue if the item is found; otherwise, False.

示例Examples

以下示例模板演示如何对不同的类型使用 contains:The following example template shows how to use contains with different types:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToTest": {
            "type": "string",
            "defaultValue": "OneTwoThree"
        },
        "objectToTest": {
            "type": "object",
            "defaultValue": {"one": "a", "two": "b", "three": "c"}
        },
        "arrayToTest": {
            "type": "array",
            "defaultValue": ["one", "two", "three"]
        }
    },
    "resources": [
    ],
    "outputs": {
        "stringTrue": {
            "type": "bool",
            "value": "[contains(parameters('stringToTest'), 'e')]"
        },
        "stringFalse": {
            "type": "bool",
            "value": "[contains(parameters('stringToTest'), 'z')]"
        },
        "objectTrue": {
            "type": "bool",
            "value": "[contains(parameters('objectToTest'), 'one')]"
        },
        "objectFalse": {
            "type": "bool",
            "value": "[contains(parameters('objectToTest'), 'a')]"
        },
        "arrayTrue": {
            "type": "bool",
            "value": "[contains(parameters('arrayToTest'), 'three')]"
        },
        "arrayFalse": {
            "type": "bool",
            "value": "[contains(parameters('arrayToTest'), 'four')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringTruestringTrue BoolBool TrueTrue
stringFalsestringFalse BoolBool FalseFalse
objectTrueobjectTrue BoolBool TrueTrue
objectFalseobjectFalse BoolBool FalseFalse
arrayTruearrayTrue BoolBool TrueTrue
arrayFalsearrayFalse BoolBool FalseFalse

dataUridataUri

dataUri(stringToConvert)

将一个值转换为数据 URI。Converts a value to a data URI.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToConvertstringToConvert Yes stringstring 要转换为数据 URI 的值。The value to convert to a data URI.

返回值Return value

格式为数据 URI 的字符串。A string formatted as a data URI.

示例Examples

以下示例模板将值转换为数据 URI,然后将数据 URI 转换为字符串:The following example template converts a value to a data URI, and converts a data URI to a string:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToTest": {
            "type": "string",
            "defaultValue": "Hello"
        },
        "dataFormattedString": {
            "type": "string",
            "defaultValue": "data:;base64,SGVsbG8sIFdvcmxkIQ=="
        }
    },
    "resources": [],
    "outputs": {
        "dataUriOutput": {
            "value": "[dataUri(parameters('stringToTest'))]",
            "type" : "string"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[dataUriToString(parameters('dataFormattedString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
dataUriOutputdataUriOutput StringString data:text/plain;charset=utf8;base64,SGVsbG8=data:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutputtoStringOutput StringString Hello, World!Hello, World!

dataUriToStringdataUriToString

dataUriToString(dataUriToConvert)

将采用数据 URI 格式的值转换为字符串。Converts a data URI formatted value to a string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
dataUriToConvertdataUriToConvert Yes stringstring 要转换的数据 URI 值。The data URI value to convert.

返回值Return value

包含转换后的值的字符串。A string containing the converted value.

示例Examples

以下示例模板将值转换为数据 URI,然后将数据 URI 转换为字符串:The following example template converts a value to a data URI, and converts a data URI to a string:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToTest": {
            "type": "string",
            "defaultValue": "Hello"
        },
        "dataFormattedString": {
            "type": "string",
            "defaultValue": "data:;base64,SGVsbG8sIFdvcmxkIQ=="
        }
    },
    "resources": [],
    "outputs": {
        "dataUriOutput": {
            "value": "[dataUri(parameters('stringToTest'))]",
            "type" : "string"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[dataUriToString(parameters('dataFormattedString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
dataUriOutputdataUriOutput StringString data:text/plain;charset=utf8;base64,SGVsbG8=data:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutputtoStringOutput StringString Hello, World!Hello, World!

emptyempty

empty(itemToTest)

确定数组、对象或字符串是否为空。Determines if an array, object, or string is empty.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
itemToTestitemToTest Yes 数组、对象或字符串array, object, or string 要检查是否为空的值。The value to check if it's empty.

返回值Return value

如果该值为空,则返回 True;否则返回 FalseReturns True if the value is empty; otherwise, False.

示例Examples

以下示例模板检查某个数组、对象和字符串是否为空。The following example template checks whether an array, object, and string are empty.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testArray": {
            "type": "array",
            "defaultValue": []
        },
        "testObject": {
            "type": "object",
            "defaultValue": {}
        },
        "testString": {
            "type": "string",
            "defaultValue": ""
        }
    },
    "resources": [
    ],
    "outputs": {
        "arrayEmpty": {
            "type": "bool",
            "value": "[empty(parameters('testArray'))]"
        },
        "objectEmpty": {
            "type": "bool",
            "value": "[empty(parameters('testObject'))]"
        },
        "stringEmpty": {
            "type": "bool",
            "value": "[empty(parameters('testString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayEmptyarrayEmpty BoolBool TrueTrue
objectEmptyobjectEmpty BoolBool TrueTrue
stringEmptystringEmpty BoolBool TrueTrue

endsWithendsWith

endsWith(stringToSearch, stringToFind)

确定某个字符串是否以某个值结尾。Determines whether a string ends with a value. 比较不区分大小写。The comparison is case-insensitive.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToSearchstringToSearch Yes stringstring 包含要查找的项的值。The value that contains the item to find.
stringToFindstringToFind Yes stringstring 要查找的值。The value to find.

返回值Return value

如果字符串的最后一个或多个字符与该值匹配,则为 True;否则为 FalseTrue if the last character or characters of the string match the value; otherwise, False.

示例Examples

以下示例模板演示如何使用 startsWith 和 endsWith 函数:The following example template shows how to use the startsWith and endsWith functions:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "startsTrue": {
            "value": "[startsWith('abcdef', 'ab')]",
            "type" : "bool"
        },
        "startsCapTrue": {
            "value": "[startsWith('abcdef', 'A')]",
            "type" : "bool"
        },
        "startsFalse": {
            "value": "[startsWith('abcdef', 'e')]",
            "type" : "bool"
        },
        "endsTrue": {
            "value": "[endsWith('abcdef', 'ef')]",
            "type" : "bool"
        },
        "endsCapTrue": {
            "value": "[endsWith('abcdef', 'F')]",
            "type" : "bool"
        },
        "endsFalse": {
            "value": "[endsWith('abcdef', 'e')]",
            "type" : "bool"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
startsTruestartsTrue BoolBool TrueTrue
startsCapTruestartsCapTrue BoolBool TrueTrue
startsFalsestartsFalse BoolBool FalseFalse
endsTrueendsTrue BoolBool TrueTrue
endsCapTrueendsCapTrue BoolBool TrueTrue
endsFalseendsFalse BoolBool FalseFalse

firstfirst

first(arg1)

返回字符串的第一个字符,或数组的第一个元素。Returns the first character of the string, or first element of the array.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或字符串array or string 要检索第一个元素或字符的值。The 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

以下示例模板演示如何对数组和字符串使用 first 函数。The following example template shows how to use the first function with an array and string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "arrayToTest": {
            "type": "array",
            "defaultValue": ["one", "two", "three"]
        }
    },
    "resources": [
    ],
    "outputs": {
        "arrayOutput": {
            "type": "string",
            "value": "[first(parameters('arrayToTest'))]"
        },
        "stringOutput": {
            "type": "string",
            "value": "[first('One Two Three')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput StringString oneone
stringOutputstringOutput StringString OO

formatformat

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

基于输入值创建带格式的字符串。Creates a formatted string from input values.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
formatStringformatString Yes stringstring 复合格式字符串。The composite format string.
arg1arg1 Yes 字符串、整数或布尔值string, integer, or boolean 要包含在带格式字符串中的值。The value to include in the formatted string.
其他参数additional arguments No 字符串、整数或布尔值string, integer, or boolean 要包含在带格式字符串中的附加值。Additional values to include in the formatted string.

备注Remarks

使用此函数来为模板中的字符串设置格式。Use this function to format a string in your template. 此函数使用的格式设置选项与 .NET 中的 System.String.Format 方法相同。It uses the same formatting options as the System.String.Format method in .NET.

示例Examples

以下示例模板演示如何使用 format 函数。The following example template shows how to use the format function.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "greeting": {
            "type": "string",
            "defaultValue": "Hello"
        },
        "name": {
            "type": "string",
            "defaultValue": "User"
        },
        "numberToFormat": {
            "type": "int",
            "defaultValue": 8175133
        }
    },
    "resources": [
    ],
    "outputs": {
        "formatTest": {
            "type": "string",
            "value": "[format('{0}, {1}. Formatted number: {2:N0}', parameters('greeting'), parameters('name'), parameters('numberToFormat'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
formatTestformatTest StringString Hello, User。Hello, User. 带格式的数字:8,175,133Formatted number: 8,175,133

GUIDguid

guid(baseString, ...)

基于以参数形式提供的值创建一个采用全局唯一标识符格式的值。Creates a value in the format of a globally unique identifier based on the values provided as parameters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
baseStringbaseString Yes stringstring 哈希函数中用于创建 GUID 的值。The value used in the hash function to create the GUID.
根据需要使用其他参数additional parameters as needed No stringstring 可以添加任意数目的字符串,以创建指定唯一性级别的值。You 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. 返回的值长度为 36 个字符。The returned value is 36 characters long. 此值并非全局唯一。It isn't globally unique. 若要创建不是基于该参数哈希值的新 GUID,请使用 newGuid 函数。To create a new GUID that isn't based on that hash value of the parameters, use the newGuid function.

以下示例演示如何使用 guid 创建常用级别唯一值。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

包含 36 个字符的全局唯一标识符格式的字符串。A string containing 36 characters in the format of a globally unique identifier.

示例Examples

以下示例模板从 guid 返回结果:The following example template returns results from guid:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "variables": {},
    "resources": [],
    "outputs": {
        "guidPerSubscription": {
            "value": "[guid(subscription().subscriptionId)]",
            "type": "string"
        },
        "guidPerResourceGroup": {
            "value": "[guid(resourceGroup().id)]",
            "type": "string"
        },
        "guidPerDeployment": {
            "value": "[guid(resourceGroup().id, deployment().name)]",
            "type": "string"
        }
    }
}

indexOfindexOf

indexOf(stringToSearch, stringToFind)

返回字符串中某个值的第一个位置。Returns the first position of a value within a string. 比较不区分大小写。The comparison is case-insensitive.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToSearchstringToSearch Yes stringstring 包含要查找的项的值。The value that contains the item to find.
stringToFindstringToFind Yes stringstring 要查找的值。The value to find.

返回值Return value

一个整数,表示要查找的项的位置。An integer that represents the position of the item to find. 该值从零开始。The value is zero-based. 如果未找到该项,则返回 -1。If the item isn't found, -1 is returned.

示例Examples

以下示例模板演示如何使用 indexOf 和 lastIndexOf 函数:The following example template shows how to use the indexOf and lastIndexOf functions:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "firstT": {
            "value": "[indexOf('test', 't')]",
            "type" : "int"
        },
        "lastT": {
            "value": "[lastIndexOf('test', 't')]",
            "type" : "int"
        },
        "firstString": {
            "value": "[indexOf('abcdef', 'CD')]",
            "type" : "int"
        },
        "lastString": {
            "value": "[lastIndexOf('abcdef', 'AB')]",
            "type" : "int"
        },
        "notFound": {
            "value": "[indexOf('abcdef', 'z')]",
            "type" : "int"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
firstTfirstT intInt 00
lastTlastT intInt 33
firstStringfirstString intInt 22
lastStringlastString intInt 00
notFoundnotFound intInt -1-1

lastlast

last (arg1)

返回字符串的最后一个字符,或数组的最后一个元素。Returns last character of the string, or the last element of the array.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或字符串array or string 要检索最后一个元素或字符的值。The 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

以下示例模板演示如何对数组和字符串使用 last 函数。The following example template shows how to use the last function with an array and string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "arrayToTest": {
            "type": "array",
            "defaultValue": ["one", "two", "three"]
        }
    },
    "resources": [
    ],
    "outputs": {
        "arrayOutput": {
            "type": "string",
            "value": "[last(parameters('arrayToTest'))]"
        },
        "stringOutput": {
            "type": "string",
            "value": "[last('One Two Three')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput StringString threethree
stringOutputstringOutput StringString ee

lastIndexOflastIndexOf

lastIndexOf(stringToSearch, stringToFind)

返回字符串中某个值的最后一个位置。Returns the last position of a value within a string. 比较不区分大小写。The comparison is case-insensitive.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToSearchstringToSearch Yes stringstring 包含要查找的项的值。The value that contains the item to find.
stringToFindstringToFind Yes stringstring 要查找的值。The value to find.

返回值Return value

一个整数,表示要查找的项的最后一个位置。An integer that represents the last position of the item to find. 该值从零开始。The value is zero-based. 如果未找到该项,则返回 -1。If the item isn't found, -1 is returned.

示例Examples

以下示例模板演示如何使用 indexOf 和 lastIndexOf 函数:The following example template shows how to use the indexOf and lastIndexOf functions:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "firstT": {
            "value": "[indexOf('test', 't')]",
            "type" : "int"
        },
        "lastT": {
            "value": "[lastIndexOf('test', 't')]",
            "type" : "int"
        },
        "firstString": {
            "value": "[indexOf('abcdef', 'CD')]",
            "type" : "int"
        },
        "lastString": {
            "value": "[lastIndexOf('abcdef', 'AB')]",
            "type" : "int"
        },
        "notFound": {
            "value": "[indexOf('abcdef', 'z')]",
            "type" : "int"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
firstTfirstT intInt 00
lastTlastT intInt 33
firstStringfirstString intInt 22
lastStringlastString intInt 00
notFoundnotFound intInt -1-1

lengthlength

length(string)

返回字符串中的字符数、数组中的元素数或对象中的根级属性数。Returns the number of characters in a string, elements in an array, or root-level properties in an object.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组、字符串或对象array, string, or object 用于获取元素数的数组、用于获取字符数的字符串,或用于获取根级属性数的对象。The 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

以下示例模板演示如何对数组和字符串使用 length:The following example template shows how to use length with an array and string:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "arrayToTest": {
            "type": "array",
            "defaultValue": [
                "one",
                "two",
                "three"
            ]
        },
        "stringToTest": {
            "type": "string",
            "defaultValue": "One Two Three"
        },
        "objectToTest": {
            "type": "object",
            "defaultValue": {
                "propA": "one",
                "propB": "two",
                "propC": "three",
                "propD": {
                    "propD-1": "sub",
                    "propD-2": "sub"
                }
            }
        }
    },
    "resources": [],
    "outputs": {
        "arrayLength": {
            "type": "int",
            "value": "[length(parameters('arrayToTest'))]"
        },
        "stringLength": {
            "type": "int",
            "value": "[length(parameters('stringToTest'))]"
        },
        "objectLength": {
            "type": "int",
            "value": "[length(parameters('objectToTest'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayLengtharrayLength intInt 33
stringLengthstringLength intInt 1313
objectLengthobjectLength intInt 44

newGuidnewGuid

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.

备注Remarks

只能在表达式中对参数的默认值使用此函数。You can only use this function within an expression for the default value of a parameter. 在模板中的其他任何位置使用此函数都会返回错误。Using this function anywhere else in a template returns an error. 不允许在模板的其他部分使用该函数,因为每次调用该函数,都会返回不同的值。The function isn't allowed in other parts of the template because it returns a different value each time it's called. 使用相同的参数部署同一模板不能可靠地生成相同的结果。Deploying the same template with the same parameters wouldn't reliably produce the same results.

newGuid 函数不同于 guid 函数,因为它不采用任何参数。The newGuid function differs from the guid function because it doesn't take any parameters. 每次结合相同的参数调用 guid 都会返回相同的标识符。When you call guid with the same parameter, it returns the same identifier each time. 需要为特定的环境可靠地生成相同的 GUID 时,请使用 guid。Use guid when you need to reliably generate the same GUID for a specific environment. 如果每次需要不同的标识符(例如,将资源部署到测试环境),请使用 newGuid。Use newGuid when you need a different identifier each time, such as deploying resources to a test environment.

newGuid 函数会使用 .NET Framework 中的 Guid 结构生成全局唯一标识符。The newGuid function uses the Guid structure in the .NET Framework to generate the globally unique identifier.

如果使用相应的选项来重新部署以前已成功的部署,而以前的部署包含一个使用 newGuid 的参数,则不会重新评估该参数,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. 无需构造唯一的名称,可以结合 uniqueString 使用 newGuid 来创建唯一的名称。Rather than constructing unique names, you can use newGuid with uniqueString to create unique names.

重新部署依赖于 newGuid 函数提供默认值的模板时请保持谨慎。Be careful redeploying a template 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

包含 36 个字符的全局唯一标识符格式的字符串。A string containing 36 characters in the format of a globally unique identifier.

示例Examples

以下示例模板演示一个带有新标识符的参数。The following example template shows a parameter with a new identifier.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "guidValue": {
            "type": "string",
            "defaultValue": "[newGuid()]"
        }
    },
    "resources": [
    ],
    "outputs": {
        "guidOutput": {
            "type": "string",
            "value": "[parameters('guidValue')]"
        }
    }
}

上述示例的输出根据每个部署的不同而异,但类似于:The output from the preceding example varies for each deployment but will be similar to:

名称Name 类型Type ValueValue
guidOutputguidOutput stringstring b76a51fc-bd72-4a77-b9a2-3c29e7d2e551b76a51fc-bd72-4a77-b9a2-3c29e7d2e551

以下示例使用 newGuid 函数创建存储帐户的唯一名称。The following example uses the newGuid function to create a unique name for a storage account. 此模板可能适用于其中的存储帐户生存期较短且未重新部署的测试环境。This template might work for test environment where the storage account exists for a short time and isn't redeployed.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "guidValue": {
            "type": "string",
            "defaultValue": "[newGuid()]"
        }
    },
    "variables": {
        "storageName": "[concat('storage', uniqueString(parameters('guidValue')))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageName')]",
            "location": "China North",
            "apiVersion": "2018-07-01",
            "sku":{
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "properties": {}
        }
    ],
    "outputs": {
        "nameOutput": {
            "type": "string",
            "value": "[variables('storageName')]"
        }
    }
}

上述示例的输出根据每个部署的不同而异,但类似于:The output from the preceding example varies for each deployment but will be similar to:

名称Name 类型Type ValueValue
nameOutputnameOutput stringstring storagenziwvyru7uxiestoragenziwvyru7uxie

padLeftpadLeft

padLeft(valueToPad, totalLength, paddingCharacter)

通过向左侧添加字符直至到达指定的总长度返回右对齐的字符串。Returns a right-aligned string by adding characters to the left until reaching the total specified length.

参数Parameters

参数Parameter 必须Required 类型Type 说明Description
valueToPadvalueToPad Yes 字符串或整数string or int 要右对齐的值。The value to right-align.
totalLengthtotalLength Yes intint 返回字符串中的字符总数。The total number of characters in the returned string.
paddingCharacterpaddingCharacter No 单个字符single character 要用于向左填充直到达到总长度的字符。The 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 template shows how to pad the user-provided parameter value by adding the zero character until it reaches the total number of characters.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "123"
        }
    },
    "resources": [],
    "outputs": {
        "stringOutput": {
            "type": "string",
            "value": "[padLeft(parameters('testString'),10,'0')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringOutputstringOutput StringString 00000001230000000123

replacereplace

replace(originalString, oldString, newString)

返回其中某个字符串的所有实例均替换为另一个字符串的新字符串。Returns a new string with all instances of one string replaced by another string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
originalStringoriginalString Yes stringstring 包含某一个字符串的所有实例均替换为另一个字符串的值。The value that has all instances of one string replaced by another string.
oldStringoldString Yes stringstring 要从原始字符串中删除的字符串。The string to be removed from the original string.
newStringnewString Yes stringstring 要添加以替代已删除字符串的字符串。The string to add in place of the removed string.

返回值Return value

包含被替换字符的字符串。A string with the replaced characters.

示例Examples

以下示例模板演示如何从用户提供的字符串中删除所有短划线,以及如何将字符串的一部分替换为其他字符串。The following example template shows how to remove all dashes from the user-provided string, and how to replace part of the string with another string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "123-123-1234"
        }
    },
    "resources": [],
    "outputs": {
        "firstOutput": {
            "type": "string",
            "value": "[replace(parameters('testString'),'-', '')]"
        },
        "secondOutput": {
            "type": "string",
            "value": "[replace(parameters('testString'),'1234', 'xxxx')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
firstOutputfirstOutput StringString 12312312341231231234
secondOutputsecondOutput StringString 123-123-xxxx123-123-xxxx

skipskip

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.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
originalValueoriginalValue Yes 数组或字符串array or string 用于跳过的数组或字符串。The array or string to use for skipping.
numberToSkipnumberToSkip Yes intint 要跳过的元素数或字符数。The number of elements or characters to skip. 如果此值小于或等于 0,则返回值中的所有元素或字符。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 template skips the specified number of elements in the array, and the specified number of characters in a string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testArray": {
            "type": "array",
            "defaultValue": [
                "one",
                "two",
                "three"
            ]
        },
        "elementsToSkip": {
            "type": "int",
            "defaultValue": 2
        },
        "testString": {
            "type": "string",
            "defaultValue": "one two three"
        },
        "charactersToSkip": {
            "type": "int",
            "defaultValue": 4
        }
    },
    "resources": [],
    "outputs": {
        "arrayOutput": {
            "type": "array",
            "value": "[skip(parameters('testArray'),parameters('elementsToSkip'))]"
        },
        "stringOutput": {
            "type": "string",
            "value": "[skip(parameters('testString'),parameters('charactersToSkip'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput ArrayArray ["three"]["three"]
stringOutputstringOutput StringString two threetwo three

splitsplit

split(inputString, delimiter)

返回包含输入字符串的子字符串的字符串数组,其中的子字符串使用指定的分隔符进行分隔。Returns an array of strings that contains the substrings of the input string that are delimited by the specified delimiters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
inputStringinputString Yes stringstring 要拆分的字符串。The string to split.
delimiterdelimiter Yes 字符串或字符串数组string or array of strings 用于拆分字符串的分隔符。The delimiter to use for splitting the string.

返回值Return value

字符串数组。An array of strings.

示例Examples

以下示例模板使用逗号以及使用逗号或分号拆分输入字符串。The following example template splits the input string with a comma, and with either a comma or a semi-colon.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "firstString": {
            "type": "string",
            "defaultValue": "one,two,three"
        },
        "secondString": {
            "type": "string",
            "defaultValue": "one;two,three"
        }
    },
    "variables": {
        "delimiters": [ ",", ";" ]
    },
    "resources": [],
    "outputs": {
        "firstOutput": {
            "type": "array",
            "value": "[split(parameters('firstString'),',')]"
        },
        "secondOutput": {
            "type": "array",
            "value": "[split(parameters('secondString'),variables('delimiters'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
firstOutputfirstOutput ArrayArray ["one", "two", "three"]["one", "two", "three"]
secondOutputsecondOutput ArrayArray ["one", "two", "three"]["one", "two", "three"]

startsWithstartsWith

startsWith(stringToSearch, stringToFind)

确定某个字符串是否以某个值开头。Determines whether a string starts with a value. 比较不区分大小写。The comparison is case-insensitive.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToSearchstringToSearch Yes stringstring 包含要查找的项的值。The value that contains the item to find.
stringToFindstringToFind Yes stringstring 要查找的值。The value to find.

返回值Return value

如果字符串的最前面一个或多个字符与该值匹配,则为 True;否则为 FalseTrue if the first character or characters of the string match the value; otherwise, False.

示例Examples

以下示例模板演示如何使用 startsWith 和 endsWith 函数:The following example template shows how to use the startsWith and endsWith functions:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "startsTrue": {
            "value": "[startsWith('abcdef', 'ab')]",
            "type" : "bool"
        },
        "startsCapTrue": {
            "value": "[startsWith('abcdef', 'A')]",
            "type" : "bool"
        },
        "startsFalse": {
            "value": "[startsWith('abcdef', 'e')]",
            "type" : "bool"
        },
        "endsTrue": {
            "value": "[endsWith('abcdef', 'ef')]",
            "type" : "bool"
        },
        "endsCapTrue": {
            "value": "[endsWith('abcdef', 'F')]",
            "type" : "bool"
        },
        "endsFalse": {
            "value": "[endsWith('abcdef', 'e')]",
            "type" : "bool"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
startsTruestartsTrue BoolBool TrueTrue
startsCapTruestartsCapTrue BoolBool TrueTrue
startsFalsestartsFalse BoolBool FalseFalse
endsTrueendsTrue BoolBool TrueTrue
endsCapTrueendsCapTrue BoolBool TrueTrue
endsFalseendsFalse BoolBool FalseFalse

stringstring

string(valueToConvert)

将指定的值转换为字符串。Converts the specified value to a string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
valueToConvertvalueToConvert Yes 任意Any 要转换为字符串的值。The 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 template shows how to convert different types of values to strings:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testObject": {
            "type": "object",
            "defaultValue": {
                "valueA": 10,
                "valueB": "Example Text"
            }
        },
        "testArray": {
            "type": "array",
            "defaultValue": [
                "a",
                "b",
                "c"
            ]
        },
        "testInt": {
            "type": "int",
            "defaultValue": 5
        }
    },
    "resources": [],
    "outputs": {
        "objectOutput": {
            "type": "string",
            "value": "[string(parameters('testObject'))]"
        },
        "arrayOutput": {
            "type": "string",
            "value": "[string(parameters('testArray'))]"
        },
        "intOutput": {
            "type": "string",
            "value": "[string(parameters('testInt'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
objectOutputobjectOutput StringString {"valueA":10,"valueB":"Example Text"}{"valueA":10,"valueB":"Example Text"}
arrayOutputarrayOutput StringString ["a","b","c"]["a","b","c"]
intOutputintOutput StringString 55

substringsubstring

substring(stringToParse, startIndex, length)

返回从指定的字符位置开始且包含指定数量的字符的子字符串。Returns a substring that starts at the specified character position and contains the specified number of characters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToParsestringToParse Yes stringstring 从中提取子字符串的原始字符串。The original string from which the substring is extracted.
startIndexstartIndex No intint 子字符串的从零开始的字符位置。The zero-based starting character position for the substring.
lengthlength No intint 子字符串的字符数。The number of characters for the substring. 必须引用该字符串内的一个位置。Must refer to a location within the string. 必须为零或更大值。Must be zero or greater.

返回值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. 索引参数:“0”,长度参数:“11”,字符串参数长度:“10”。”。The index parameter: '0', the length parameter: '11', the length of the string parameter: '10'.".

"parameters": {
    "inputString": { "type": "string", "value": "1234567890" }
},
"variables": {
    "prefix": "[substring(parameters('inputString'), 0, 11)]"
}

示例Examples

以下示例模板从参数中提取子字符串。The following example template extracts a substring from a parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "one two three"
        }
    },
    "resources": [],
    "outputs": {
        "substringOutput": {
            "value": "[substring(parameters('testString'), 4, 3)]",
            "type": "string"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
substringOutputsubstringOutput StringString twotwo

taketake

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.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
originalValueoriginalValue Yes 数组或字符串array or string 要从中提取元素的数组或字符串。The array or string to take the elements from.
numberToTakenumberToTake Yes intint 要提取的元素或字符数。The number of elements or characters to take. 如果此值为 0 或更小,则返回一个空数组或字符串。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 template takes the specified number of elements from the array, and characters from a string.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testArray": {
            "type": "array",
            "defaultValue": [
                "one",
                "two",
                "three"
            ]
        },
        "elementsToTake": {
            "type": "int",
            "defaultValue": 2
        },
        "testString": {
            "type": "string",
            "defaultValue": "one two three"
        },
        "charactersToTake": {
            "type": "int",
            "defaultValue": 2
        }
    },
    "resources": [],
    "outputs": {
        "arrayOutput": {
            "type": "array",
            "value": "[take(parameters('testArray'),parameters('elementsToTake'))]"
        },
        "stringOutput": {
            "type": "string",
            "value": "[take(parameters('testString'),parameters('charactersToTake'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput ArrayArray ["one", "two"]["one", "two"]
stringOutputstringOutput StringString onon

toLowertoLower

toLower(stringToChange)

将指定的字符串转换为小写。Converts the specified string to lower case.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToChangestringToChange Yes stringstring 要转换为小写的值。The value to convert to lower case.

返回值Return value

已转换为小写的字符串。The string converted to lower case.

示例Examples

以下示例模板将参数值转换为小写和大写。The following example template converts a parameter value to lower case and to upper case.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "One Two Three"
        }
    },
    "resources": [],
    "outputs": {
        "toLowerOutput": {
            "value": "[toLower(parameters('testString'))]",
            "type": "string"
        },
        "toUpperOutput": {
            "type": "string",
            "value": "[toUpper(parameters('testString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
toLowerOutputtoLowerOutput StringString one two threeone two three
toUpperOutputtoUpperOutput StringString ONE TWO THREEONE TWO THREE

toUppertoUpper

toUpper(stringToChange)

将指定的字符串转换为大写。Converts the specified string to upper case.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToChangestringToChange Yes stringstring 要转换为大写的值。The value to convert to upper case.

返回值Return value

已转换为大写的字符串。The string converted to upper case.

示例Examples

以下示例模板将参数值转换为小写和大写。The following example template converts a parameter value to lower case and to upper case.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "One Two Three"
        }
    },
    "resources": [],
    "outputs": {
        "toLowerOutput": {
            "value": "[toLower(parameters('testString'))]",
            "type": "string"
        },
        "toUpperOutput": {
            "type": "string",
            "value": "[toUpper(parameters('testString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
toLowerOutputtoLowerOutput StringString one two threeone two three
toUpperOutputtoUpperOutput StringString ONE TWO THREEONE TWO THREE

trimtrim

trim (stringToTrim)

从指定的字符串中删除所有前导和尾随空白字符。Removes all leading and trailing white-space characters from the specified string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToTrimstringToTrim Yes stringstring 要剪裁的值。The value to trim.

返回值Return value

不带前导和尾随空白字符的字符串。The string without leading and trailing white-space characters.

示例Examples

以下示例模板裁剪掉参数中的空白字符。The following example template trims the white-space characters from the parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testString": {
            "type": "string",
            "defaultValue": "    one two three   "
        }
    },
    "resources": [],
    "outputs": {
        "return": {
            "type": "string",
            "value": "[trim(parameters('testString'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
returnreturn StringString one two threeone two three

uniqueStringuniqueString

uniqueString (baseString, ...)

根据作为参数提供的值创建确定性哈希字符串。Creates a deterministic hash string based on the values provided as parameters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
baseStringbaseString Yes stringstring 哈希函数中用于创建唯一字符串的值。The value used in the hash function to create a unique string.
根据需要使用其他参数additional parameters as needed No stringstring 可以添加任意数目的字符串,以创建指定唯一性级别的值。You 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. 返回的值长度为 13 个字符。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

以下示例演示如何使用 uniqueString 创建通用级别唯一值。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.

"resources": [{
    "name": "[concat('storage', uniqueString(resourceGroup().id))]",
    "type": "Microsoft.Storage/storageAccounts",
    ...

如果每次部署模板都需要创建新的唯一名称并且不希望更新资源,可以结合 uniqueString 使用 utcNow 函数。If you need to create a new unique name each time you deploy a template, 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. 有关示例,请参阅 utcNowFor an example, see utcNow.

返回值Return value

包含 13 个字符的字符串。A string containing 13 characters.

示例Examples

以下示例模板从唯一字符串返回结果:The following example template returns results from uniquestring:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "uniqueRG": {
            "value": "[uniqueString(resourceGroup().id)]",
            "type" : "string"
        },
        "uniqueDeploy": {
            "value": "[uniqueString(resourceGroup().id, deployment().name)]",
            "type" : "string"
        }
    }
}

uriuri

uri (baseUri, relativeUri)

通过组合 baseUri 和 relativeUri 字符串来创建绝对 URI。Creates an absolute URI by combining the baseUri and the relativeUri string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
baseUribaseUri Yes stringstring 基本 uri 字符串。The base uri string. 请注意观察与处理尾随斜杠 ('/') 有关的行为,如下表所述。Take care to observe the behavior regarding the handling of the trailing slash ('/'), as described following this table.
relativeUrirelativeUri Yes stringstring 要添加到基本 uri 字符串的相对 uri 字符串。The relative uri string to add to the base uri string.
  • 如果 baseUri 以尾随斜杠结尾,则结果就是 baseUri 后跟 relativeUri。If baseUri ends in a trailing slash, the result is simply baseUri followed by relativeUri.

  • 如果 baseUri 不以尾随斜杠结尾,则会出现以下两种情况之一。If baseUri does not end in a trailing slash one of two things happens.

    • 如果 baseUri 根本没有斜杠(除了前面的“/”之外),则结果就是 baseUri 后跟 relativeUriIf baseUri has no slashes at all (aside from the "//" near the front) the result is simply baseUri followed by relativeUri.

    • 如果 baseUri 包含一些斜杠,但不是以斜杠结尾,则从最后一个斜杠开始的所有内容都将从 baseUri 中删除,结果是 baseUri 后跟 relativeUri。If baseUri has some slashes, but doesn't end with a slash, everything from the last slash onward is removed from baseUri and the result is baseUri followed by relativeUri.

下面是一些示例:Here are some examples:

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

有关完整的详细信息,请按照 RFC 3986 第 5 部分中的说明解析 baseUrirelativeUri 参数。For complete details, the baseUri and relativeUri parameters are resolved as specified in RFC 3986, section 5.

返回值Return value

表示基值和相对值的绝对 URI 的字符串。A string representing the absolute URI for the base and relative values.

示例Examples

以下示例演示如何根据父模板的值构造嵌套模板的链接。The following example shows how to construct a link to a nested template based on the value of the parent template.

"templateLink": "[uri(deployment().properties.templateLink.uri, 'nested/azuredeploy.json')]"

以下示例模板演示如何使用 uri、uriComponent 和 uriComponentToString:The following example template shows how to use uri, uriComponent, and uriComponentToString:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "variables": {
        "uriFormat": "[uri('http://contoso.com/resources/', 'nested/azuredeploy.json')]",
        "uriEncoded": "[uriComponent(variables('uriFormat'))]"
    },
    "resources": [
    ],
    "outputs": {
        "uriOutput": {
            "type": "string",
            "value": "[variables('uriFormat')]"
        },
        "componentOutput": {
            "type": "string",
            "value": "[variables('uriEncoded')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[uriComponentToString(variables('uriEncoded'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
uriOutputuriOutput StringString http://contoso.com/resources/nested/azuredeploy.json
componentOutputcomponentOutput StringString http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputtoStringOutput StringString http://contoso.com/resources/nested/azuredeploy.json

uriComponenturiComponent

uricomponent(stringToEncode)

将 URI 编码。Encodes a URI.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
stringToEncodestringToEncode Yes stringstring 要编码的值。The value to encode.

返回值Return value

URI 编码值的字符串。A string of the URI encoded value.

示例Examples

以下示例模板演示如何使用 uri、uriComponent 和 uriComponentToString:The following example template shows how to use uri, uriComponent, and uriComponentToString:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "variables": {
        "uriFormat": "[uri('http://contoso.com/resources/', 'nested/azuredeploy.json')]",
        "uriEncoded": "[uriComponent(variables('uriFormat'))]"
    },
    "resources": [
    ],
    "outputs": {
        "uriOutput": {
            "type": "string",
            "value": "[variables('uriFormat')]"
        },
        "componentOutput": {
            "type": "string",
            "value": "[variables('uriEncoded')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[uriComponentToString(variables('uriEncoded'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
uriOutputuriOutput StringString http://contoso.com/resources/nested/azuredeploy.json
componentOutputcomponentOutput StringString http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputtoStringOutput StringString http://contoso.com/resources/nested/azuredeploy.json

uriComponentToStringuriComponentToString

uriComponentToString(uriEncodedString)

返回 URI 编码值的字符串。Returns a string of a URI encoded value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
uriEncodedStringuriEncodedString Yes stringstring 要转换为字符串的 URI 编码值。The URI encoded value to convert to a string.

返回值Return value

URI 编码值的解码后字符串。A decoded string of URI encoded value.

示例Examples

以下示例模板演示如何使用 uri、uriComponent 和 uriComponentToString:The following example template shows how to use uri, uriComponent, and uriComponentToString:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "variables": {
        "uriFormat": "[uri('http://contoso.com/resources/', 'nested/azuredeploy.json')]",
        "uriEncoded": "[uriComponent(variables('uriFormat'))]"
    },
    "resources": [
    ],
    "outputs": {
        "uriOutput": {
            "type": "string",
            "value": "[variables('uriFormat')]"
        },
        "componentOutput": {
            "type": "string",
            "value": "[variables('uriEncoded')]"
        },
        "toStringOutput": {
            "type": "string",
            "value": "[uriComponentToString(variables('uriEncoded'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
uriOutputuriOutput StringString http://contoso.com/resources/nested/azuredeploy.json
componentOutputcomponentOutput StringString http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutputtoStringOutput StringString http://contoso.com/resources/nested/azuredeploy.json

后续步骤Next steps