Skip to content

Instantly share code, notes, and snippets.

@naxmefy
Last active August 29, 2015 14:17
Show Gist options
  • Save naxmefy/899685a556a9f4a12ff4 to your computer and use it in GitHub Desktop.
Save naxmefy/899685a556a9f4a12ff4 to your computer and use it in GitHub Desktop.
Unity3D StringManipulation (Format) like Underscore template
#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