Monday 27 April 2009

Scala - String splitting & reversing

A way of splitting and reversing a string by spaces:

from this input => input this from

val z2 = List.fromString(s, ' ')
z2.reduceRight[String]((a,b) => b+a )

To reverse every character of a String do this: (Note there is a list.reverse function I am choosing not to use)

def stringFlip2(s : String) : String = {
val z2 = List.fromString(s)
List.toString(reverse(z2))
}

def reverse[T](l: List[T]): List[T] = {
l.foldLeft(List[T]())((r, h) => h :: r)
}

No comments:

Post a Comment