From 8f958e250589acbb6b534261f6c78697796e6372 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 1 Sep 2006 01:05:23 +0000 Subject: [PATCH] Added truncate(). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4353 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/StringUtil.as | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/as/com/threerings/util/StringUtil.as b/src/as/com/threerings/util/StringUtil.as index b73a6effa..e035d26dd 100644 --- a/src/as/com/threerings/util/StringUtil.as +++ b/src/as/com/threerings/util/StringUtil.as @@ -50,5 +50,23 @@ public class StringUtil { return mx.utils.StringUtil.trim(str); } + + /** + * Truncate the specified String if it is longer than maxLength. + * The string will be truncated at a position such that it is + * maxLength chars long after the addition of the 'append' String. + * + * @param append a String to add to the truncated String only after + * truncation. + */ + public static function truncate ( + s :String, maxLength :int, append :String = "") :String + { + if ((s == null) || (s.length <= maxLength)) { + return s; + } else { + return s.substring(0, maxLength - append.length) + append; + } + } } }