当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala String compareToIgnoreCase()用法及代码示例


compareToIgnoreCase()方法用于比较两个字符串,例如compareTo方法,但是在比较时忽略大小写(大写或小写)差异。

函数定义:int compareToIgnoreCase(String str)
返回类型:It is same as compareTo() method but here only case difference is ignored.

示例:1#


// Scala program of compareToIgnoreCase() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a String 
        val m1 = "Nidhi"
          
        // Applying compareToIgnoreCase() method 
        val result = m1.compareToIgnoreCase("Nidhi") 
          
        // Displays output 
        println(result) 
      
    } 
} 
输出:
0

范例2:

// Scala program of compareToIgnoreCase() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a String 
        val m1 = "Nidhi"
          
        // Applying compareToIgnoreCase() method 
        val result = m1.compareToIgnoreCase("NiDhi") 
          
        // Displays output 
        println(result) 
      
    } 
} 
输出:
0

在此,即使大小写不同,两个字符串也被视为相同。
范例3:

// Scala program of compareToIgnoreCase() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a String 
        val m1 = "Nidhi"
      
        // Applying compareToIgnoreCase() method 
        val result = m1.compareToIgnoreCase("NiDh") 
      
        // Displays output 
        println(result) 
      
    } 
} 
输出:
1


相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Scala String compareToIgnoreCase() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。