Last active
August 29, 2015 14:17
-
-
Save naxmefy/899685a556a9f4a12ff4 to your computer and use it in GitHub Desktop.
Unity3D StringManipulation (Format) like Underscore template
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
#pragma strict | |
import System.Text.RegularExpressions; | |
public class StringManipulation { | |
private static var interpolate : String = "\\{(.+?)\\}"; | |
/** | |
* Example use: StringManipulation.Format("{name} {weather}", { | |
"name": "Tom", | |
"weather": "Rain" | |
}); -> "Tom Rain" | |
*/ | |
public static function Format(value:String, args:Hashtable) : String { | |
var match = Regex.Match(value, interpolate); | |
while(match.Success) { | |
var key : String = match.Groups[1].Value; | |
var replacement : String = args[key] as String; | |
if(replacement.length > 0) { | |
value = value.Replace(match.Value, replacement); | |
} | |
// Debug.Log("Value: "+value); | |
// Debug.Log("Key: "+key); | |
// Debug.Log("Replacement: "+replacement); | |
// Debug.Log("Match: " + match.Value + " Success: "+match.Success); | |
// Debug.Log("============================================"); | |
match = match.NextMatch(); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment