[Scala Foundation]--string's operations

Source: Internet
Author: User
Tags lowercase
Operating environment: jdk1.7, scala-2.10.4
Study

/**
  * Document: This type of function----> Test string
  * USER:YANGJF
  * DATE:2016/8/14  8:07
  * Image Array, Strings are not direct sequences, but they can be converted to sequences,
And they also support all sequence operations on strings here are some examples where you can understand how to operate on a string */Objectteststring {defMain (args:array[string]) {ValCc=teststring () println ("Uppercase conversion:"+cc.changeupper ("ABCDE")) println ("Lowercase conversion:"+cc.changelower ("Qwer")) println ("Sequential reversal:"+CC.REVERSESTR ("123456789")) println ("Deletes the element at the specified location:"+cc.dropelement ("0987654321")) println ("Gets the element at the specified location:"+cc.substring ("1234567")) println ("Get sequence:"+CC.SEQUNCESTR ("QAZXSW")) println ("add element:"+cc.addelement ("1234qwer")) println ("gets the subscript where the element is located:"+cc.getindex ("a")) println ("Gets the element value for the specified subscript:"+cc.getcharat (2)) println ("contains the specified character:"+cc.containsis ("G")//The return value is True or false}Case ClassTestString () {Lazy ValStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ" Uppercase ConversiondefChangeupper (str:string) ={Str.map (_.toupper)}//Lowercase conversiondefChangelower (str:string) ={Str.map (_.tolower)}//InvertdefReversestr (str:string) ={Str.reverse}//Culling elementdefDropelement (str:string) ={Str.drop (2)//Remove the element labeled 2}//intercept the element at the specified positiondefSubString (str:string) ={str.slice (1,3)//before package}//Get sequencedefSequncestr (str:string) ={ValS:seq[char] = str s}//Get sequencedefAddElement (str:string) ={Str.concat ("Hello")}//Get subscriptdefGetIndex (str2:string) ={str.indexof (STR2)}//Gets the element with the specified subscriptdefGetcharat (I:int) ={Str.charat (i)}//contains this elementdefContainsis (foo:string) ={str.contains (foo)}}}
Run result value:
Uppercase conversion: ABCDE
lowercase conversion: qwer
order reversal: 987654321
Delete the element at the specified position: 87654321
Gets the element at the specified position: the
fetch sequence: QAZXSW
add element: 1234qwerhello
Gets the subscript where the element is located: 0
Gets the element value that specifies the subscript: C
contains the specified character: true
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.