[scalaベース]--Stringの操作

7960 ワード

    :jdk1.7、scala-2.10.4
package study

/**
  * Document:    ---->     
  * User: yangjf
  * Date: 2016/8/14  8:07
  *    ,          ,           ,
                                           
  */
object TestString {
  def main(args: Array[String]) {
    val cc=TestString()
    println("    :"+cc.changeUpper("abcde"))
    println("    :"+cc.changeLower("QWER"))
    println("    :"+cc.reverseStr("123456789"))
    println("         :"+cc.dropElement("0987654321"))
    println("         :"+cc.subString("1234567"))
    println("    :"+cc.sequnceStr("qazxsw"))
    println("    :"+cc.addElement("1234qwer"))
    println("         :"+cc.getIndex("a"))
    println("          :"+cc.getCharAt(2))
    println("        :"+cc.containsIs("g"))//    true  false
  }

  case class TestString(){
    lazy val str="abcdefghijklmnopqrstuvwxyz"
    //    
    def changeUpper(str:String)={
      str.map(_.toUpper)
    }
    //    
    def changeLower(str:String)={
      str.map(_.toLower)
    }
    //  
    def reverseStr(str:String)={
      str.reverse
    }
    //    
    def dropElement(str:String)={
      str.drop(2)//     2     
    }
    //         
    def subString(str:String)={
      str.slice(1,3)//     
    }
    //    
    def sequnceStr(str:String)={
      val s: Seq[Char] = str
      s
    }
    //    
    def addElement(str:String)={
     str.concat("hello")
    }
    //    
    def getIndex(str2:String)={
       str.indexOf(str2)
    }
    //         
    def getCharAt(i:Int)={
      str.charAt(i)
    }
    //       
    def containsIs(foo:String) ={
      str.contains(foo)
    }
  }
}

    :ABCDE
    :qwer
    :987654321
         :87654321
         :23
    :qazxsw
    :1234qwerhello
         :0
          :c
        :true