Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created September 1, 2010 08:01
Show Gist options
  • Select an option

  • Save brianhsu/560384 to your computer and use it in GitHub Desktop.

Select an option

Save brianhsu/560384 to your computer and use it in GitHub Desktop.
val content = "我是陶喆\n我是游鍚堃\n這是測試"
println (normalizeString(content))
def normalizeString (string: String) = string.flatMap(escapeNonBig5Character _)
def escapeNonBig5Character (character: Char): String = {
def isNotBig5 (character: Char) = character.toString.getBytes("Big5")(0) == '?' &&
character != '?'
isNotBig5(character) match {
case true => "&#%d;" format(character.toInt) // 不是 Big5 的字元就轉成字元參引
case false => character.toString // 不然的話維持原樣
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment