Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Sunday, 14 June 2009

String parsing thru lists

Filter numbers from strings.

println( "there are 7 numbers 8 in 2 here"
.toList.filter(Character.isDigit) )


Load the first word only.

println(("first word only".toList.takeWhile(c => c != ' ') ).mkString)

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)
}