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

Bicep 比较运算符

比较运算符比较值并返回 truefalse。 若要运行这些示例,请使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件

运算符 名称
>= 大于或等于
> 大于
<= 小于或等于
< 小于
== 等于
!= 不等于
=~ 等于不区分大小写
!~ 不等于不区分大小写

大于或等于 >=

operand1 >= operand2

计算第一个值是否大于或等于第二个值。

操作数

操作数 类型 说明
operand1 整数、字符串 进行比较的第一个值。
operand2 整数、字符串 进行比较的第二个值。

返回值

如果第一个值大于或等于第二个值,返回 true。 否则将返回 false

示例

比较一对整数和一对字符串。

param firstInt int = 10
param secondInt int = 5

param firstString string = 'A'
param secondString string = 'A'

output intGtE bool = firstInt >= secondInt
output stringGtE bool = firstString >= secondString

示例中的输出:

名称 类型
intGtE boolean
stringGtE boolean

大于 >

operand1 > operand2

计算第一个值是否大于第二个值。

操作数

操作数 类型 说明
operand1 整数、字符串 进行比较的第一个值。
operand2 整数、字符串 进行比较的第二个值。

返回值

如果第一个值大于第二个值,返回 true。 否则将返回 false

示例

比较一对整数和一对字符串。

param firstInt int = 10
param secondInt int = 5

param firstString string = 'bend'
param secondString string = 'band'

output intGt bool = firstInt > secondInt
output stringGt bool = firstString > secondString

示例中的输出:

“bend”中的“e”使第一个字符串更大 。

名称 类型
intGt boolean
stringGt boolean

小于或等于 <=

operand1 <= operand2

计算第一个值是否小于或等于第二个值。

操作数

操作数 类型 说明
operand1 整数、字符串 进行比较的第一个值。
operand2 整数、字符串 进行比较的第二个值。

返回值

如果第一个值小于或等于第二个值,返回 true。 否则将返回 false

示例

比较一对整数和一对字符串。

param firstInt int = 5
param secondInt int = 10

param firstString string = 'demo'
param secondString string = 'demo'

output intLtE bool = firstInt <= secondInt
output stringLtE bool = firstString <= secondString

示例中的输出:

名称 类型
intLtE boolean
stringLtE boolean

小于 <

operand1 < operand2

计算第一个值是否小于第二个值。

操作数

操作数 类型 说明
operand1 整数、字符串 进行比较的第一个值。
operand2 整数、字符串 进行比较的第二个值。

返回值

如果第一个值小于第二个值,返回 true。 否则将返回 false

示例

比较一对整数和一对字符串。

param firstInt int = 5
param secondInt int = 10

param firstString string = 'demo'
param secondString string = 'Demo'

output intLt bool = firstInt < secondInt
output stringLt bool = firstString < secondString

示例中的输出:

字符串为 true,因为小写字母小于大写字母。

名称 类型
intLt boolean
stringLt boolean

等于 ==

operand1 == operand2

计算值是否相等。

操作数

操作数 类型 说明
operand1 字符串、整数、布尔、数组、对象 进行比较的第一个值。
operand2 字符串、整数、布尔、数组、对象 进行比较的第二个值。

返回值

如果操作数都相等,返回 true。 如果操作数不同,返回 false

示例

对整数、字符串和布尔值对进行比较。

param firstInt int = 5
param secondInt int = 5

param firstString string = 'demo'
param secondString string = 'demo'

param firstBool bool = true
param secondBool bool = true

output intEqual bool = firstInt == secondInt
output stringEqual bool = firstString == secondString
output boolEqual bool = firstBool == secondBool

示例中的输出:

名称 类型
intEqual boolean
stringEqual boolean
boolEqual boolean

在比较数组时,两个数组必须具有相同的元素和顺序。 数组不需要相互分配。

var array1 = [
  1
  2
  3
]

var array2 = [
  1
  2
  3
]

var array3 = array2

var array4 = [
  3
  2
  1
]

output sameElements bool = array1 == array2 // returns true because arrays are defined with same elements
output assignArray bool = array2 == array3 // returns true because one array was defined as equal to the other array
output differentOrder bool = array4 == array1 // returns false because order of elements is different

示例中的输出:

名称 类型
sameElements bool true
assignArray bool true
differentOrder bool false

在比较对象时,属性名称和值必须相同。 不需要按相同顺序定义属性。

var object1 = {
  prop1: 'val1'
  prop2: 'val2'
}

var object2 = {
  prop1: 'val1'
  prop2: 'val2'
}

var object3 = {
  prop2: 'val2'
  prop1: 'val1'
}

var object4 = object3

var object5 = {
  prop1: 'valX'
  prop2: 'valY'
}

output sameObjects bool = object1 == object2 // returns true because both objects defined with same properties
output differentPropertyOrder bool = object3 == object2 // returns true because both objects have same properties even though order is different
output assignObject bool = object4 == object1 // returns true because one object was defined as equal to the other object
output differentValues bool = object5 == object1 // returns false because values are different

示例中的输出:

名称 类型
sameObjects bool true
differentPropertyOrder bool true
assignObject bool true
differentValues bool false

不等于 !=

operand1 != operand2

计算两个值是否不相等。

操作数

操作数 类型 说明
operand1 字符串、整数、布尔、数组、对象 进行比较的第一个值。
operand2 字符串、整数、布尔、数组、对象 进行比较的第二个值。

返回值

如果操作数不相等,返回 true。 如果操作数都相等,返回 false

示例

对整数、字符串和布尔值对进行比较。

param firstInt int = 10
param secondInt int = 5

param firstString string = 'demo'
param secondString string = 'test'

param firstBool bool = false
param secondBool bool = true

output intNotEqual bool = firstInt != secondInt
output stringNotEqual bool = firstString != secondString
output boolNotEqual bool = firstBool != secondBool

示例中的输出:

名称 类型
intNotEqual boolean
stringNotEqual boolean
boolNotEqual boolean

有关数组和对象,请参阅等于中的示例。

等于不区分大小写 =~

operand1 =~ operand2

忽略大小写以确定这两个值是否相等。

操作数

操作数 类型 说明
operand1 string 进行比较的第一个字符串。
operand2 string 进行比较的第二个字符串。

返回值

如果字符串相等,返回 true。 否则将返回 false

示例

比较使用混合大小写字母的字符串。

param firstString string = 'demo'
param secondString string = 'DEMO'

param thirdString string = 'demo'
param fourthString string = 'TEST'

output strEqual1 bool = firstString =~ secondString
output strEqual2 bool = thirdString =~ fourthString

示例中的输出:

名称 类型
strEqual1 boolean
strEqual2 boolean false

不等于不区分大小写 !~

operand1 !~ operand2

忽略大小写以确定这两个值是否不相等。

操作数

操作数 类型 说明
operand1 string 进行比较的第一个字符串。
operand2 string 进行比较的第二个字符串。

返回值

如果字符串不相等,返回 true。 否则将返回 false

示例

比较使用混合大小写字母的字符串。

param firstString string = 'demo'
param secondString string = 'TEST'

param thirdString string = 'demo'
param fourthString string = 'DeMo'

output strNotEqual1 bool = firstString !~ secondString
output strEqual2 bool = thirdString !~ fourthString

示例中的输出:

名称 类型
strNotEqual1 boolean
strNotEqual2 boolean false

后续步骤