Class CSVUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildCsvLine(Map fields, char fieldSeparator, char textSeparator, String lineBreak) static booleanescapeString(StringBuilder unescaped, char[] toEscape, char[] specialChars, boolean doubleCharacter) It escapes the passedtoEscapechar(s) within the given buffer.static booleanescapeString(StringBuilder unescaped, String[] toEscape, String[] specialChars, boolean doubleToEscape) It escapes the passedtoEscapestring(s) within the given buffer.static StringescapeString(String unescaped, char[] toEscape, boolean doubleCharacter) Escapes the passedtoEscapechar(s) in the passed String.static booleanisSeparator(char[] tokenisers, char actual) Returns false is the actual char is not a separator.static StringjoinAndEscape(List<String> cells, char[] toEscape, char delimiter, boolean escapeByDoubling) Escapes the given list of text fields and merges them to one single line using given delimiter.static booleanlineStartsWith(String line, char[] prefixes, String appender) Checks if given line starts with at least one of the given prefixes followed by given appender text.splitAndUnescape(String line, char[] delimiters, boolean escapedByDoubling) Splits a given line in fields separated by given delimiters and unescapes the fields with usage of {unescapeString(String, char[], boolean).static StringunescapeString(String escaped, char[] toUnEscape, boolean doubleCharacter) Unescapes the passedtoUnEscapechar(s) in the passed String.
-
Method Details
-
unescapeString
Unescapes the passedtoUnEscapechar(s) in the passed String. IfdoubleCharacteris set to true all double occurrence of the char(s) intoUnEscapewill be reduced to one. Else all'\'before the chars intoUnEscapeare removed.This method undo all changes to a String which are done by
escapeString(String, char[], boolean)
escapeString( unEscapeString( String, char[], boolean ), char[], boolean )
== unEscapeString( escapeString( String, char[], boolean ), char[], boolean )
== String
for the same parameters (== means here equal strings)- Parameters:
escaped- the escaped stringtoUnEscape- the chars which are to unescapeddoubleCharacter- false: '\' is removed if char occurs in char[]; true: double occurrence of char from char[] is reduced to one- Returns:
- the unescaped string
-
escapeString
Escapes the passedtoEscapechar(s) in the passed String. IfdoubleCharacteris set totrueall foundtoEscapechar(s) are doubled, else the found characters will be escaped with'\'.Example:
textseparator chars are: '"', ';'
a,";"bc -> a,"";;""bc doubleCharacter=true
a,";"bc -> a,\"\;\"bc doubleCharacter=false
If you furthermore want to wrap the string only on demand dependent of occurrence of special characters see
escapeString(StringBuilder, char[], char[], boolean).- Parameters:
unescaped- the unescaped stringtoEscape- the char(s) to be escapeddoubleCharacter- if set tofalseall found characters are escaped with'\'- Returns:
- the escaped string
-
escapeString
public static boolean escapeString(StringBuilder unescaped, char[] toEscape, char[] specialChars, boolean doubleCharacter) It escapes the passedtoEscapechar(s) within the given buffer. IfdoubleCharacteris set totrueall foundtoEscapechar(s) are doubled, else the found characters will be escaped with '\'.Difference to
escapeString(String, char[], boolean)is, that it returns an boolean to indicate whether special characters occur in buffer. So you can decide dependent on the return value whether you wrap the buffer with text separators or not.- Parameters:
unescaped- the field contenttoEscape- the char(s) to be escapedspecialChars- char(s) checked for occurrence in given stringdoubleCharacter- if set tofalseall found characters are escaped with'\'- Returns:
- true, if at least one special character occurs in buffer, false otherwise
- See Also:
-
escapeString
public static boolean escapeString(StringBuilder unescaped, String[] toEscape, String[] specialChars, boolean doubleToEscape) It escapes the passedtoEscapestring(s) within the given buffer. IfdoubleToEscapeis set totrueall foundtoEscapestring(s) are doubled, else the found strings will be escaped with '\'.Difference to
escapeString(String, char[], boolean)is, that it returns an boolean to indicate whether special strings occur in buffer. So you can decide dependent on the return value whether you wrap the buffer with text separators or not.- Parameters:
unescaped- the field contenttoEscape- the string(s) to be escapedspecialChars- string(s) checked for occurrence in given stringdoubleToEscape- if set tofalseall found strings are escaped with'\'- Returns:
- true, if at least one special string occurs in buffer, false otherwise
- See Also:
-
isSeparator
public static boolean isSeparator(char[] tokenisers, char actual) Returns false is the actual char is not a separator.- Parameters:
tokenisers- an array of chars which are separatorsactual- the actual char- Returns:
- true if actual is equal to a char from
tokenisers
-
splitAndUnescape
public static List<String> splitAndUnescape(String line, char[] delimiters, boolean escapedByDoubling) Splits a given line in fields separated by given delimiters and unescapes the fields with usage of {unescapeString(String, char[], boolean).- Parameters:
line- text to split and unescapedelimiters- the chars used for splitting and which have to be unescapedescapedByDoubling- if false '\' is removed if char occurs in given delimiters ,if true double occurrence of char from delimiters is reduced to one- Returns:
- list containing splittet and unescaped fields
- See Also:
-
joinAndEscape
public static String joinAndEscape(List<String> cells, char[] toEscape, char delimiter, boolean escapeByDoubling) Escapes the given list of text fields and merges them to one single line using given delimiter.- Parameters:
cells- list of text fields for escaping and mergingtoEscape- char(s) which have to be escaped within fieldsdelimiter- delimiter char for merging the fields to one lineescapeByDoubling- if set tofalseall found characters are escaped with'\'- Returns:
- the merged line
-
lineStartsWith
Checks if given line starts with at least one of the given prefixes followed by given appender text.- Parameters:
line- line to checkprefixes- list of line prefixes where the line has to start withappender- text which has to follow a found line prefix (can be null)- Returns:
- line starts with one of the given prefixes followed by given appender text
-
buildCsvLine
-