CamelCase in Scala
Example for versions
Scala 2.8.0-final
This example uses regular expressions twice. First one, words
, describes the words in the text; each match of it is replaced with itself, converted to lower case and capitalized. Second one, separators
, describes the spaces between words; all matches of it are replaced with empty string, i.e., just removed from the string.
import java.io.{BufferedReader, InputStreamReader}
import scala.util.matching.Regex
object Main {
def main(args: Array[String]) {
var stdin = new BufferedReader(new InputStreamReader(System.in));
var text = stdin.readLine();
val words = """([a-zA-Z]+)""".r
text = words.replaceAllIn(text, m => m.matched.toLowerCase.capitalize)
val separators = """([^a-zA-Z]+)""".r
text = separators.replaceAllIn(text, "");
println(text);
}
}
Comments
]]>blog comments powered by Disqus
]]>