-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |