tribble.util
Class Strings

java.lang.Object
  extended by tribble.util.Strings

public abstract class Strings
extends java.lang.Object

Utility methods for string processing.

Since:
2002-11-05
Version:
$Revision: 1.1 $ $Date: 2002/11/05 00:01:09 $
Author:
David R. Tribble (david@tribble.com).
Copyright 2002 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.

Method Summary
static java.lang.String[] splitCSV(java.lang.String line)
          Split a CSV text line into separate field values.
static java.lang.String[] splitCSV(java.lang.String line, boolean stripSp)
          Split a CSV text line into separate field values.
static java.lang.String toCSV(java.lang.String val)
          Convert a string into a quoted CSV field value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

splitCSV

public static java.lang.String[] splitCSV(java.lang.String line)
Split a CSV text line into separate field values. This is equivalent to the call:
    splitCSV(line, true) 

Parameters:
line - A string containing one or more text fields separated by commas (CSV).
Returns:
An array of strings, representing the CSV fields extracted from line, or null if the input line is empty.
Since:
1.1, 2002-11-05

splitCSV

public static java.lang.String[] splitCSV(java.lang.String line,
                                          boolean stripSp)
Split a CSV text line into separate field values.

Example

Consider the following CSV input text line:

    Acme & Co., Inc.,"","A, B, and C",,"He said, ""Hello.""" 
The line above is split into the following strings (fields):
    [0]: "Acme & Co."
    [1]: " Inc."
    [2]: ""
    [3]: "A, B, and C"
    [4]: ""
    [5]: "He said, \"Hello.\"" 

Parameters:
line - A string containing one or more text fields separated by commas (CSV).
stripSp - If true, leading and trailing spaces are stripped from each CSV field, otherwise the fields are left unaltered.
Returns:
An array of strings, representing the CSV fields extracted from line, or null if the input line is empty.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if the CSV text line is malformed.
Since:
1.1, 2002-11-05

toCSV

public static java.lang.String toCSV(java.lang.String val)
Convert a string into a quoted CSV field value.

Example

Consider the following input strings:

    "Acme & Co., Inc."
    "A, B, and C Company"
    ""
    "He said, \"Hello.\"" 
These are converted into the following output CSV strings:
    "\"Acme & Co., Inc.\""
    "\"A, B, and C Company\""
    "\"\""
    "\"He said, \"\"Hello.\"\"\"" 

Parameters:
val - A string containing a text field value.
Returns:
A new string containing the contents of field val, enclosed within quotes (") and with all occurrences of special characters replaced with escape sequences. This string is suitable for writing to a comma-separated-value (CSV) file.
Since:
1.1, 2002-11-05