Skip to content

Commit

Permalink
added string manipulation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Aug 16, 2012
1 parent 1a94e5c commit 13d1ac6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions strings/manipulation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.util._

val name = "Matt Aimonetti"
val email = "matt.aimonetti@email.com"
println("#head retreives the first character")
println(name.head)
println("The same thing can be achieved with #take(1) (from StringOps)")
println(name.take(1))
println("To print the last character we can use #takeRight")
println(name.takeRight(1))
println("Of course, we could have also used #substring")
println(name.substring(0,1))
println(name.substring(name.length-1, name.length))
println("On the oter hand, #drop & #dropRight can be used to 'removed' characters")
println(name.drop(1))
println(name.drop(5))
println(name.dropRight(10))
println("We could also split the string using the white space")
println(name.split(" ")(0))
println(name.split(" ")(1))
println("Split takes a string that gets converted in a regexp so you need to be careful, here is an example:")
println("array size after splitting on '.': " + "matt.aimonetti@email.com".split(".").length)
println("in this case, you want to escape the string")
println("array size after splitting on '\\.': " + email.split("\\.").length)
// Or you can quote the entire string, which makes sense in long strings
println("array size after splitting on '\\Q.\\E': "+ email.split("\\Q.\\E").length)

0 comments on commit 13d1ac6

Please sign in to comment.