你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ARM 模板的字符串函数

资源管理器提供了以下可用于在 Azure 资源管理器模板(ARM 模板)中处理字符串的函数:

提示

建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且语法更易于使用。 若要了解详细信息,请参阅字符串函数。

base64

base64(inputString)

返回输入字符串的 base64 表示形式。

在 Bicep 中,使用函数 base64

parameters

参数 必选 类型 说明
inputString 字符串 要以 base64 表示形式返回的值。

返回值

包含 base64 表示形式的字符串。

示例

以下示例演示如何使用 base64 函数:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageResourceGroup": {
      "type": "string"
    },
    "storageAccountName": {
      "type": "string"
    }
  },
  "resources": [],
  "outputs": {
    "ExistingStorage": {
      "type": "object",
      "value": "[reference(resourceId(parameters('storageResourceGroup'), 'Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2021-04-01')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
base64Output String b25lLCB0d28sIHRocmVl
toStringOutput String 一二三
toJsonOutput Object {“one”: “a”, “two”: “b”}

base64ToJson

base64ToJson(base64Value)

将 base64 表示形式转换为 JSON 对象。

在 Bicep 中,使用函数 base64ToJson

parameters

参数 必选 类型 说明
base64Value 字符串 要转换为 JSON 对象的 base64 表示形式。

返回值

一个 JSON 对象。

示例

以下示例使用 base64ToJson 函数转换 base64 值:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageResourceGroup": {
      "type": "string"
    },
    "storageAccountName": {
      "type": "string"
    }
  },
  "resources": [],
  "outputs": {
    "ExistingStorage": {
      "type": "object",
      "value": "[reference(resourceId(parameters('storageResourceGroup'), 'Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2021-04-01')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
base64Output String b25lLCB0d28sIHRocmVl
toStringOutput String 一二三
toJsonOutput Object {“one”: “a”, “two”: “b”}

base64ToString

base64ToString(base64Value)

将 base64 表示形式转换为字符串。

在 Bicep 中,使用函数 base64ToString

parameters

参数 必选 类型 说明
base64Value 字符串 要转换为字符串的 base64 表示形式。

返回值

转换后的 base64 值的字符串。

示例

以下示例使用 base64ToString 函数转换 base64 值:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageResourceGroup": {
      "type": "string"
    },
    "storageAccountName": {
      "type": "string"
    }
  },
  "resources": [],
  "outputs": {
    "ExistingStorage": {
      "type": "object",
      "value": "[reference(resourceId(parameters('storageResourceGroup'), 'Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2021-04-01')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
base64Output String b25lLCB0d28sIHRocmVl
toStringOutput String 一二三
toJsonOutput Object {“one”: “a”, “two”: “b”}

concat

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

合并多个字符串值并返回串联的字符串,或合并多个数组并返回串联的数组。

在 Bicep 中,使用字符串内插而非 concat() 函数来提高可读性。 但是,在某些情况下(如 多行字符串中的字符串替换)可能需要回退使用 concat() 函数或 replace() 函数。

parameters

参数 必选 类型 说明
arg1 字符串或数组 要串联的第一个字符串或数组。
其他参数 字符串或数组 要串联的按顺序排列的其他字符串或数组。

此函数可以采用任意数量的参数,并且可以接受参数的字符串或数组。 但是,不能同时为参数提供数组和字符串。 字符串仅与其他字符串连接。

返回值

由串联值构成的字符串或数组。

示例

以下示例演示如何合并两个字符串值并返回串联字符串:

{
  "$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": {
      "type": "string",
      "value": "[concat(parameters('prefix'), '-', uniqueString(resourceGroup().id))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
concatOutput String prefix-5yj4yjf5mbg72

以下示例演示如何合并两个数组:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
return Array ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

contains

contains(container, itemToFind)

检查数组是否包含值、对象是否包含键或字符串是否包含子字符串。 字符串比较区分大小写。 但在测试某个对象是否包含某个键时,该比较不区分大小写。

在 Bicep 中,使用函数 contains

parameters

参数 必选 类型 说明
容器 数组、对象或字符串 包含要查找的值的值。
itemToFind 字符串或整数 要查找的值。

返回值

如果找到项目,则为 True;否则为 False

示例

以下示例演示如何对不同的类型使用 contains

{
  "$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')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
stringTrue Bool True
stringFalse Bool False
objectTrue Bool True
objectFalse Bool False
arrayTrue Bool True
arrayFalse Bool False

dataUri

dataUri(stringToConvert)

将一个值转换为数据 URI。

在 Bicep 中,使用函数 dataUri

parameters

参数 必选 类型 说明
stringToConvert 字符串 要转换为数据 URI 的值。

返回值

格式为数据 URI 的字符串。

示例

以下示例将值转换为数据 URI,并将数据 URI 转换为字符串:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
dataUriOutput String data:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutput String 世界您好!

dataUriToString

dataUriToString(dataUriToConvert)

将采用数据 URI 格式的值转换为字符串。

在 Bicep 中,使用函数 dataUriToString

parameters

参数 必选 类型 说明
dataUriToConvert 字符串 要转换的数据 URI 值。

返回值

包含转换后的值的字符串。

示例

以下示例模板将值转换为数据 URI,并将数据 URI 转换为字符串:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
dataUriOutput String data:text/plain;charset=utf8;base64,SGVsbG8=
toStringOutput String 世界您好!

empty

empty(itemToTest)

确定数组、对象或字符串是否为空。

在 Bicep 中,使用函数 empty

parameters

参数 必选 类型 说明
itemToTest 数组、对象或字符串 要检查是否为空的值。

返回值

如果该值为空,则返回 True;否则返回 False

示例

以下示例检查数组、对象和字符串是否为空:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayEmpty Bool True
objectEmpty Bool True
stringEmpty Bool True

endsWith

endsWith(stringToSearch, stringToFind)

确定字符串是否以值结尾。 比较不区分大小写。

在 Bicep 中,使用函数 endsWith

parameters

参数 必选 类型 说明
stringToSearch 字符串 包含要查找的项的值。
stringToFind 字符串 要查找的值。

返回值

如果字符串最后的一个或多个字符与该值匹配,则为 True;否则为 False

示例

以下示例说明如何使用 startsWithendsWith 函数:

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

上述示例中的默认值输出为:

名称 类型
startsTrue Bool True
startsCapTrue Bool True
startsFalse Bool False
endsTrue Bool True
endsCapTrue Bool True
endsFalse Bool False

first

first(arg1)

返回字符串的第一个字符,或数组的第一个元素。 如果给定一个空字符串,则该函数会生成空字符串。 如果数组为空,则该函数会返回 null

在 Bicep 中,使用函数 first

parameters

参数 必选 类型 说明
arg1 数组或字符串 要检索第一个元素或字符的值。

返回值

第一个字符或数组中第一个元素的类型(字符串、int、数组或对象)的字符串。

示例

以下示例演示如何将 first 函数与数组和字符串一起使用:

{
  "$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')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayOutput String one
stringOutput String O

format

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

基于输入值创建带格式的字符串。

在 Bicep 中,使用函数 format

parameters

参数 必选 类型 说明
formatString 字符串 复合格式字符串。
arg1 字符串、整数或布尔值 要包含在带格式字符串中的值。
其他参数 字符串、整数或布尔值 要包含在带格式字符串中的其他值。

备注

使用此函数来为模板中的字符串设置格式。 此函数使用的格式设置选项与 .NET 中的 System.String.Format 方法相同。

示例

以下示例演示如何使用 format 函数:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
formatTest String Hello, User。 带格式的数字:8,175,133

GUID

guid(baseString, ...)

基于以参数形式提供的值创建一个采用全局唯一标识符格式的值。

在 Bicep 中,使用函数 guid

parameters

参数 必选 类型 说明
baseString 字符串 哈希函数中用于创建 GUID 的值。
根据需要使用的其他参数 字符串 可以添加任意数目的字符串,以创建指定唯一性级别的值。

备注

当需要以全局唯一标识符格式创建值时,此功能十分有用。 提供参数值,这些值用于限制结果的唯一性范围。 可以指定名称是否唯一到订阅、资源组或部署。

返回的值不是随机字符串,而是参数中哈希函数的结果。 返回的值长度为 36 个字符。 此值并非全局唯一。 若要创建不基于参数的哈希值的新 GUID,请使用该 newGuid 函数。

以下示例演示如何用于 guid 为常用级别创建唯一值:

仅对订阅唯一

"[guid(subscription().subscriptionId)]"

仅对资源组唯一

"[guid(resourceGroup().id)]"

仅对资源组的部署唯一

"[guid(resourceGroup().id, deployment().name)]"

guid 函数实现 RFC 4122 §4.3 中的算法。 可在 GuidUtility 中找到原始源,这些源进行了一些修改。 在函数实现中 guid() ,将 namespaceId 设置为 11fb06fb-712d-4ddd-98c7-e71bbd588830,并且 version 设置为 5。 通过将函数的每个参数 guid() 转换为字符串并将它们 - 作为分隔符连接来生成该值。

返回值

包含 36 个字符的全局唯一标识符格式的字符串。

示例

以下示例返回来自 guid 的结果:

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

indexOf

indexOf(stringToSearch, stringToFind)

返回字符串中某个值的第一个位置。 比较不区分大小写。

在 Bicep 中,使用函数 indexOf

parameters

参数 必选 类型 说明
stringToSearch 字符串 包含要查找的项的值。
stringToFind 字符串 要查找的值。

返回值

一个整数,表示要查找的项的位置。 该值从零开始。 如果未找到该项,则返回 -1。

示例

以下示例说明如何使用 indexOflastIndexOf 函数:

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

上述示例中的默认值输出为:

名称 类型
firstT int 0
lastT int 3
firstString int 2
lastString int 0
notFound int -1

加入

join(inputArray, delimiter)

将字符串数组联接到单个字符串中,使用分隔符分隔。

在 Bicep 中,使用函数 join

parameters

参数 必选 类型 说明
inputArray 字符串数组 要联接的一个字符串数组。
分隔符 用于拆分字符串的分隔符。

返回值

一个字符串。

示例

以下示例将输入字符串数组联接到由不同分隔符分隔的字符串中:

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

前述示例的输出为:

名称 类型
firstOutput String "one,two,three"
secondOutput String "one;two;three"

json

json(arg1)

将有效的 JSON 字符串转换为 JSON 数据类型。 有关详细信息,请参阅 json 函数。

在 Bicep 中,使用函数 json

last

last(arg1)

返回字符串的最后一个字符,或数组的最后一个元素。

在 Bicep 中,使用函数 last

parameters

参数 必选 类型 说明
arg1 数组或字符串 要检索最后一个元素或字符的值。

返回值

最后一个字符的字符串,或者数组中最后一个元素的类型(字符串、整数、数组或对象)。

示例

以下示例演示如何将 last 函数与数组和字符串一起使用:

{
  "$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')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayOutput String three
stringOutput String e

lastIndexOf

lastIndexOf(stringToSearch, stringToFind)

返回字符串中某个值的最后一个位置。 比较不区分大小写。

在 Bicep 中,使用函数 lastIndexOf

parameters

参数 必选 类型 说明
stringToSearch 字符串 包含要查找的项的值。
stringToFind 字符串 要查找的值。

返回值

一个整数,表示要查找的项的最后一个位置。 该值从零开始。 如果未找到该项,则返回 -1。

示例

以下示例说明如何使用 indexOflastIndexOf 函数:

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

上述示例中的默认值输出为:

名称 类型
firstT int 0
lastT int 3
firstString int 2
lastString int 0
notFound int -1

length

length(string)

返回字符串中的字符数、数组中的元素数或对象中的根级属性数。

在 Bicep 中,使用函数 length

parameters

参数 必选 类型 说明
arg1 数组、字符串或对象 用于获取元素数的数组、用于获取字符数的字符串,或用于获取根级属性数的对象。

返回值

一个整数。

示例

以下示例演示如何将 length 函数与数组和字符串一起使用:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayLength int 3
stringLength int 13
objectLength int 4

newGuid

newGuid()

以全局唯一标识符的格式返回一个值。 此函数只能在参数的默认值中使用。

在 Bicep 中,使用函数 newGuid

备注

只能在表达式中对参数的默认值使用此函数。 在模板中的其他任何位置使用此函数都会返回错误。 不允许在模板的其他部分使用该函数,因为每次调用该函数,都会返回不同的值。 使用相同的参数部署同一模板不能可靠地生成相同的结果。

newGuid 函数与 guid 函数不同,因为它不采用任何参数。 每次结合相同的参数调用 guid 都会返回相同的标识符。 需要为特定的环境可靠地生成相同的 GUID 时,请使用 guid。 如果每次需要不同的标识符(例如,将资源部署到测试环境),请使用 newGuid。

newGuid 函数会使用 .NET Framework 中的 Guid 结构生成全局唯一标识符。

如果使用 此选项重新部署早期成功的部署,其中早期部署 包含使用 newGuid的参数,则不会再次评估该参数。 相反,回滚部署会自动重复使用先前部署中的参数值。

在测试环境中,可能需要重复部署生存期较短的资源。 可以使用 newGuid 创建 uniqueString 唯一名称,而不是构造唯一名称。

请谨慎重新部署依赖于 newGuid 函数的默认值的模板。 如果重新部署且不提供参数的值,则会重新评估该函数。 若要更新现有的资源而不是新建资源,请传入以前部署中的参数值。

返回值

包含 36 个字符的全局唯一标识符格式的字符串。

示例

以下示例显示了具有新标识符的参数:

{
  "$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')]"
    }
  }
}

上述示例的输出根据每个部署的不同而异,但类似于:

名称 类型
guidOutput 字符串 b76a51fc-bd72-4a77-b9a2-3c29e7d2e551

以下示例使用 newGuid 函数创建存储帐户的唯一名称。 此模板适用于短期内存在存储帐户且不会再次部署的测试环境:

{
  "$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",
      "apiVersion": "2021-04-01",
      "name": "[variables('storageName')]",
      "location": "West US",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "nameOutput": {
      "type": "string",
      "value": "[variables('storageName')]"
    }
  }
}

上述示例的输出根据每个部署的不同而异,但类似于:

名称 类型
nameOutput 字符串 storagenziwvyru7uxie

padLeft

padLeft(valueToPad, totalLength, paddingCharacter)

通过向左侧添加字符直至到达指定的总长度返回右对齐的字符串。

在 Bicep 中,使用函数 padLeft

parameters

参数 必选 类型 说明
valueToPad 字符串或整数 要右对齐的值。
totalLength int 返回字符串中的字符总数。
paddingCharacter 单个字符 要用于向左填充直到达到总长度的字符。 默认值为空格。

如果原始字符串的长度超过要填充的字符数,则不会添加任何字符。

返回值

一个字符串,其中至少包含指定的字符数。

示例

以下示例演示如何添加 个字符,直到达到用户提供的参数值的总字符数:

{
  "$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')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
stringOutput String 0000000123

替换

replace(originalString, oldString, newString)

返回其中某个字符串的所有实例均替换为另一个字符串的新字符串。

在 Bicep 中,使用函数 replace

parameters

参数 必选 类型 说明
originalString 字符串 包含某一个字符串的所有实例均替换为另一个字符串的值。
oldString 字符串 要从原始字符串中删除的字符串。
newString 字符串 要添加以替代已删除字符串的字符串。

返回值

包含被替换字符的字符串。

示例

以下示例演示如何从用户提供的字符串中删除所有短划线,以及如何将部分字符串替换为另一个字符串:

{
  "$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')]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
firstOutput String 1231231234
secondOutput String 123-123-xxxx

跳过

skip(originalValue, numberToSkip)

返回一个字符串,其中包含指定字符数后面的所有字符;或者返回一个数组,其中包含指定元素数后面的所有元素。

在 Bicep 中,使用函数 skip

parameters

参数 必选 类型 说明
originalValue 数组或字符串 用于跳过的数组或字符串。
numberToSkip int 要跳过的元素数或字符数。 如果此值小于或等于 0,则返回值中的所有元素或字符。 如果此值大于数组或字符串的长度,则返回空数组或字符串。

返回值

数组或字符串。

示例

以下示例跳过数组中的指定元素数和字符串中的指定字符数:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayOutput Array ["three"]
stringOutput String 两个三个

拆分

split(inputString, delimiter)

返回包含输入字符串的子字符串的字符串数组,其中的子字符串使用指定的分隔符进行分隔。

在 Bicep 中,使用函数 split

parameters

参数 必选 类型 说明
inputString 字符串 要拆分的字符串。
分隔符 字符串或字符串数组 用于拆分字符串的分隔符。

返回值

字符串数组。

示例

以下示例使用逗号拆分输入字符串,并使用逗号或分号拆分以下字符串:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
firstOutput Array [“one”, “two”, “three”]
secondOutput Array [“one”, “two”, “three”]

startsWith

startsWith(stringToSearch, stringToFind)

确定字符串是否以值开头。 比较不区分大小写。

在 Bicep 中,使用函数 startsWith

parameters

参数 必选 类型 说明
stringToSearch 字符串 包含要查找的项的值。
stringToFind 字符串 要查找的值。

返回值

如果字符串最前面的一个或多个字符与该值匹配,则为 True;否则为 False

示例

以下示例说明如何使用 startsWithendsWith 函数:

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

上述示例中的默认值输出为:

名称 类型
startsTrue Bool True
startsCapTrue Bool True
startsFalse Bool False
endsTrue Bool True
endsCapTrue Bool True
endsFalse Bool False

字符串

string(valueToConvert)

将指定的值转换为字符串。

在 Bicep 中,使用函数 string

parameters

参数 必选 类型 说明
valueToConvert 任意 要转换为字符串的值。 可以转换任何类型的值,包括对象和数组。

返回值

转换后的值的字符串。

示例

以下示例演示如何将不同类型的值转换为字符串:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
objectOutput String {“valueA”:10,“valueB”:“示例文本”}
arrayOutput String ["a","b","c"]
intOutput String 5

substring

substring(stringToParse, startIndex, length)

返回从指定的字符位置开始且包含指定数量的字符的子字符串。

在 Bicep 中,使用函数 substring

parameters

参数 必选 类型 说明
stringToParse 字符串 从中提取子字符串的原始字符串。
startIndex int 子字符串的从零开始的字符位置。
length int 子字符串的字符数。 必须引用该字符串内的一个位置。 必须为零或更大值。 如果省略,则返回字符串从起始位置开始的剩余部分。

返回值

子字符串。 或者,如果长度为零,则为空字符串。

备注

当子字符串延伸超出字符串末尾或长度小于零时,函数将失败。 以下示例失败并出现错误“索引和长度参数必须引用字符串内的位置。 索引参数:“0”,长度参数:“11”,字符串参数的长度:“10”。”

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

示例

以下示例从参数中提取子字符串:

{
  "$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": {
      "type": "string",
      "value": "[substring(parameters('testString'), 4, 3)]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
substringOutput String two

take

take(originalValue, numberToTake)

返回数组或字符串。 数组包含指定数目的元素(从数组开头算起)。 字符串包含指定数目的字符(从字符串开头算起)。

在 Bicep 中,使用函数 take

parameters

参数 必选 类型 说明
originalValue 数组或字符串 要从中提取元素的数组或字符串。
numberToTake int 要提取的元素或字符数。 如果此值为 0 或更小,则返回一个空数组或字符串。 如果此值大于给定数组或字符串的长度,则返回数组或字符串中的所有元素。

返回值

数组或字符串。

示例

以下示例采用数组中的指定元素数和字符串中的字符数:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
arrayOutput Array [“one”, “two”]
stringOutput String on

toLower

toLower(stringToChange)

将指定的字符串转换为小写。

在 Bicep 中,使用函数 toLower

parameters

参数 必选 类型 说明
stringToChange 字符串 要转换为小写的值。

返回值

已转换为小写的字符串。

示例

以下示例将参数值转换为小写和大写:

{
  "$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": {
      "type": "string",
      "value": "[toLower(parameters('testString'))]"
    },
    "toUpperOutput": {
      "type": "string",
      "value": "[toUpper(parameters('testString'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
toLowerOutput String 一二三
toUpperOutput String 一二三

toUpper

toUpper(stringToChange)

将指定的字符串转换为大写。

在 Bicep 中,使用函数 toUpper

parameters

参数 必选 类型 说明
stringToChange 字符串 要转换为大写的值。

返回值

已转换为大写的字符串。

示例

以下示例将参数值转换为小写和大写:

{
  "$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": {
      "type": "string",
      "value": "[toLower(parameters('testString'))]"
    },
    "toUpperOutput": {
      "type": "string",
      "value": "[toUpper(parameters('testString'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
toLowerOutput String 一二三
toUpperOutput String 一二三

剪裁

trim(stringToTrim)

从指定的字符串中删除所有前导和尾随空白字符。

在 Bicep 中,使用函数 trim

parameters

参数 必选 类型 说明
stringToTrim 字符串 要剪裁的值。

返回值

不带前导和尾随空白字符的字符串。

示例

以下示例从参数中剪裁空格字符:

{
  "$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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
return String 一二三

uniqueString

uniqueString(baseString, ...)

根据作为参数提供的值创建确定性哈希字符串。

在 Bicep 中,使用函数 uniqueString

parameters

参数 必选 类型 说明
baseString 字符串 哈希函数中用于创建唯一字符串的值。
根据需要使用的其他参数 字符串 可以添加任意数目的字符串,以创建指定唯一性级别的值。

备注

当需要创建资源的唯一名称时,此函数很有帮助。 提供参数值,这些值用于限制结果的唯一性范围。 可以指定名称是否唯一到订阅、资源组或部署。

返回的值不是随机字符串,而是哈希函数的结果。 返回的值长度为 13 个字符。 此值并非全局唯一。 你可能想要将值与命名约定中的前缀组合在一起,以创建有意义的名称。 以下示例显示了返回值的格式。 实际值取决于提供的参数。

tcvhiyu5h2o5o

以下示例演示如何用于 uniqueString 为常用级别创建唯一值:

仅对订阅唯一

"[uniqueString(subscription().subscriptionId)]"

仅对资源组唯一

"[uniqueString(resourceGroup().id)]"

仅对资源组的部署唯一

"[uniqueString(resourceGroup().id, deployment().name)]"

以下示例演示显示如何根据资源组创建存储帐户的唯一名称。 在资源组中,如果名称的构造方式相同,则名称并不唯一:

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

如果需要在每次部署模板时创建新的唯一名称,并且不打算更新资源,则可以将 utcNow 函数与 一起使用 uniqueString。 可以在测试环境中使用此方法。 有关示例,请参阅 utcNow

返回值

包含 13 个字符的字符串。

示例

以下示例返回来自 uniquestring 的结果:

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

uri

uri(baseUri, relativeUri)

通过组合 baseUri 和 relativeUri 字符串来创建绝对 URI。

在 Bicep 中,使用函数 uri

parameters

参数 必选 类型 说明
baseUri 字符串 基本 uri 字符串。 请注意观察与处理尾随斜杠 (/) 有关的行为,如此表后面所述。
relativeUri 字符串 要添加到基本 uri 字符串的相对 uri 字符串。
  • 如果 baseUri 以尾随斜杠结尾,则结果只是 baseUri 后跟 relativeUri。 如果 relativeUri 还以前导斜杠开头,则尾部斜杠和前导斜杠将合并为一个斜杠。

  • 如果 baseUri 不以尾随斜杠结尾,则会出现以下两种情况之一。

    • 如果 baseUri 根本没有斜杠(除了靠近前面的“//”之外),则结果就是 baseUri 后跟 relativeUri

    • 如果 baseUri 包含一些斜杠,但不是以斜杠结尾,则从最后一个斜杠开始的所有内容都将从 baseUri 中删除,结果是 baseUri 后跟 relativeUri

以下是一些示例:

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/', '/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

有关完整详细信息, baseUri 解析参数, relativeUriRFC 3986 第 5 节中所述。

返回值

表示基值和相对值的绝对 URI 的字符串。

示例

以下示例演示如何基于父模板的值构造指向嵌套模板的链接:

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

以下示例模板演示如何使用 uriuriComponent以及 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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
uriOutput String http://contoso.com/resources/nested/azuredeploy.json
componentOutput String http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutput String http://contoso.com/resources/nested/azuredeploy.json

uriComponent

uricomponent(stringToEncode)

将 URI 编码。

在 Bicep 中,使用函数 uriComponent

parameters

参数 必选 类型 说明
stringToEncode 字符串 要编码的值。

返回值

URI 编码值的字符串。

示例

以下示例模板演示如何使用 uriuriComponent以及 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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
uriOutput String http://contoso.com/resources/nested/azuredeploy.json
componentOutput String http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutput String http://contoso.com/resources/nested/azuredeploy.json

uriComponentToString

uriComponentToString(uriEncodedString)

返回 URI 编码值的字符串。

在 Bicep 中,使用函数 uriComponentToString

parameters

参数 必选 类型 说明
uriEncodedString 字符串 要转换为字符串的 URI 编码值。

返回值

URI 编码值的解码后字符串。

示例

以下示例演示如何使用 uriuriComponent以及 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'))]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型
uriOutput String http://contoso.com/resources/nested/azuredeploy.json
componentOutput String http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json
toStringOutput String http://contoso.com/resources/nested/azuredeploy.json

后续步骤