Created
August 23, 2013 11:17
-
-
Save ryoppy/6318172 to your computer and use it in GitHub Desktop.
TextIncrementer
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
/** | |
* 文字列内の数値をインクリメントする | |
* ex) TextIncrementer("abc123efg456hij").++() // abc124efg457hij | |
* ex) TextIncrementer("あいう1 2 3えお").++() // あいう2 3 4えお | |
* @param t インクリメントしたい文字列 | |
* @param incrValue いくつインクリメントするか | |
* @return インクリメントした文字列 | |
*/ | |
case class TextIncrementer(t: String, incrValue: Int = 1) { | |
def ++(i: Int = 0, n: String = ""): String = { | |
n match { | |
case "" => ++(1, t.slice(0, 1)) | |
case _ if (i > t.length) => n // end | |
case _ => { | |
//println(i, n) | |
n.takeRight(1).exists(_.isDigit) match { // abc1 1が数値か | |
case true => { | |
val c = sequenceNumberCount(i) | |
val nn = n.dropRight(1) // abc1 -> abc | |
val inc = t.slice(i - 1, c).toInt + incrValue | |
++(c + 1, nn + inc + t.slice(c, c + 1)) | |
} | |
case false => ++(i + 1, n + t.slice(i, i + 1)) | |
} | |
} | |
} | |
} | |
private def sequenceNumberCount(i: Int): Int = { | |
t.slice(i, i + 1).exists(_.isDigit) match { | |
case true => sequenceNumberCount(i + 1) | |
case false => i | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment