Created
July 21, 2011 12:31
-
-
Save toshikazuhorii/1097098 to your computer and use it in GitHub Desktop.
#daimonscala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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