[scala基礎]--String的操作

來源:互聯網
上載者:User
運行環境: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
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.