compareTo()方法用於將指定的字符值與參數列表中的值進行比較。與比較方法相同。
函數定義: def compareTo(arg0: Character): Int
返回類型: It returns Int. Where, if the stated character value is greater than ‘x’ then a positive integer is returned, if its less than ‘x’ then a negative integer is returned and if its equal to ‘x’ then zero is returned.
示例:1#
// Scala program of compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying compareTo() method
val result = 'a'.compareTo('A')
// Displays output
println(result)
}
}
輸出:
32
示例:2#
// Scala program of compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying compareTo() method
val result = 'B'.compareTo('B')
// Displays output
println(result)
}
}
輸出:
0
相關用法
- Scala Char to(end: Char, step: Char)用法及代碼示例
- Scala Int compareTo()用法及代碼示例
- Scala Int CompareTo用法及代碼示例
- Scala Float compareTo()用法及代碼示例
- Scala String compareTo()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Scala Char compareTo() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。