From 5777070d57e85db2a755f3afec1de9203c7a1d0d Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 9 Dec 2004 01:33:33 +0000 Subject: [PATCH] Added parseLongArray(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@1540 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/StringUtil.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index de79ac64..17f383b2 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -903,6 +903,34 @@ public class StringUtil return vals; } + /** + * Parses an array of longs from it's string representation. The + * array should be represented as a bare list of numbers separated by + * commas, for example: + * + *
+     * 25, 17125141422, 21, 99
+     * 
+ * + * Any inability to parse the long array will result in the function + * returning null. + */ + public static long[] parseLongArray (String source) + { + StringTokenizer tok = new StringTokenizer(source, ","); + long[] vals = new long[tok.countTokens()]; + for (int i = 0; tok.hasMoreTokens(); i++) { + try { + // trim the whitespace from the token + String token = tok.nextToken().trim(); + vals[i] = Long.parseLong(token); + } catch (NumberFormatException nfe) { + return null; + } + } + return vals; + } + /** * Parses an array of floats from it's string representation. The * array should be represented as a bare list of numbers separated by