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 true
stringGtE boolean true

大於 >

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 true
stringGt boolean true

小於或等於 <=

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 true
stringLtE boolean true

小於 <

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 true
stringLt boolean true

等於 ==

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 true
stringEqual boolean true
boolEqual boolean true

比較陣列時,兩個陣列的元素和順序必須相同。 陣列無須指派給彼此。

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 true
stringNotEqual boolean true
boolNotEqual boolean true

針對陣列和物件,請參閱等於中的範例。

等於不區分大小寫 =~

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 true
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 true
strNotEqual2 boolean false

下一步