Skip to content

Instantly share code, notes, and snippets.

@toshikazuhorii
Created July 21, 2011 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshikazuhorii/1097098 to your computer and use it in GitHub Desktop.
Save toshikazuhorii/1097098 to your computer and use it in GitHub Desktop.
#daimonscala
class StringWithReverses(str: String) {
def reverseOrder(): String = {
var rev = new StringBuilder(str.length)
(0 until str.length) foreach { (i) => rev.append(str.charAt(str.length-1-i)) }
rev.toString()
}
def reverseCases(): String = {
var rev = new StringBuilder(str.length)
(0 until str.length) foreach { (i) =>
str.charAt(i) match {
case c if c.isLowerCase => rev.append(c.toUpperCase)
case c if c.isUpperCase => rev.append(c.toLowerCase)
case c => rev.append(c)
}
}
rev.toString()
}
}
implicit def convertToReversable(str: String) = new StringWithReverses(str)
"AbcDEf123".reverseOrder() // "321fEDcbA"
"AbcDEf123".reverseCases() // "aBCdeF123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment