Added join(String[]).

git-svn-id: https://samskivert.googlecode.com/svn/trunk@882 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
shaper
2002-10-31 01:24:10 +00:00
parent f79b5828a7
commit daaa6994e2
@@ -1,5 +1,5 @@
// //
// $Id: StringUtil.java,v 1.39 2002/10/16 00:44:48 mdb Exp $ // $Id: StringUtil.java,v 1.40 2002/10/31 01:24:10 shaper Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -698,14 +698,35 @@ public class StringUtil
return tokens; return tokens;
} }
/**
* Joins an array of strings into a single string separated by commas.
*/
public static String join (String[] values)
{
return joinEscaped(values, false);
}
/** /**
* Joins an array of strings into a single string, separated by * Joins an array of strings into a single string, separated by
* commas, and escaping commas that occur in the individual string * commas, and escaping commas that occur in the individual string
* values such that a subsequent call to {@link #parseStringArray} * values such that a subsequent call to {@link #parseStringArray}
* would recreated the string array properly. Any elements in the * would recreate the string array properly. Any elements in the
* values array that are null will be treated as an empty string. * values array that are null will be treated as an empty string.
*/ */
public static String joinEscaped (String[] values) public static String joinEscaped (String[] values)
{
return joinEscaped(values, true);
}
/**
* Joins an array of strings into a single string, separated by
* commas, and optionally escaping commas that occur in the individual
* string values such that a subsequent call to {@link
* #parseStringArray} would recreate the string array properly. Any
* elements in the values array that are null will be treated as an
* empty string.
*/
public static String joinEscaped (String[] values, boolean escape)
{ {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
int vlength = values.length; int vlength = values.length;
@@ -714,7 +735,7 @@ public class StringUtil
buf.append(", "); buf.append(", ");
} }
String value = (values[i] == null) ? "" : values[i]; String value = (values[i] == null) ? "" : values[i];
buf.append(replace(value, ",", ",,")); buf.append((escape) ? replace(value, ",", ",,") : value);
} }
return buf.toString(); return buf.toString();
} }