Home

String

String

These are the most important String methods (here, S and T are strings):

In addition, strings provide all the common methods of all sequences—you can think of a string as a sequence of characters.

For example:

scala> val S = "CS109 is nice"
S: java.lang.String = CS109 is nice
scala> S.contains("ice")
res0: Boolean = true
scala> S.indexOf("ice")
res1: Int = 10
scala> S.indexOf("rain")
res2: Int = -1
scala> S.replace('i', '#')
res4: java.lang.String = CS109 #s n#ce
scala> S.split("\\s+")
res5: Array[java.lang.String] = Array(CS109, is, nice)
scala> S.toLowerCase
res6: java.lang.String = cs109 is nice
scala> S.toUpperCase
res7: java.lang.String = CS109 IS NICE
scala> S.substring(5)
res8: java.lang.String = " is nice"
scala> S.substring(5,8)
res9: java.lang.String = " is"
scala> S.reverse
res10: String = ecin si 901SC
scala> val F = "%5s %3d %-3d %g"
F: java.lang.String = %5s %3d %-3d %g
scala> F.format("cs206", 12, 3, math.Pi)
res11: String = cs206  12 3   3.14159