Java String Quote quoteCSVValue(String value, String sep)

Here you can find the source of quoteCSVValue(String value, String sep)

Description

Quotes a value in CSV format converter.

License

Open Source License

Parameter

Parameter Description
value value to quote
sep CSV separator, to check whether the value contains it

Return

the value quoted in CSV format

Declaration

public static String quoteCSVValue(String value, String sep) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  ww  w .  ja v  a 2s .  c o m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    public static final String QUOTE = "\"";

    /**
     * Quotes a value in CSV format converter. Here are the rules:
     * <ol><li>Fields with given separator must be delimited with double-quote
     * characters.</li>
     * <li>Fields that contain double quote characters must be
     * surrounded by double-quotes, and the embedded double-quotes must each be
     * represented by a pair of consecutive double quotes.</li>
     * <li>A field that contains embedded line-breaks must be surrounded by
     * double-quotes.</li>
     * <li>Fields with leading or trailing spaces must be delimited with
     * double-quote characters.</li>
     * <li>Null values are represented by empty strings without quotes</li>
     * 
     * @param value value to quote
     * @param sep CSV separator, to check whether the value contains it
     * @return the value quoted in CSV format
     */
    public static String quoteCSVValue(String value, String sep) {
        if (value == null) {
            return null;
        } else if (value.length() == 0) {
            return QUOTE + QUOTE;
        }

        // escape quotes
        value = value.replaceAll(QUOTE, QUOTE + QUOTE);

        boolean needQuote = false;
        needQuote = (value.indexOf(sep) != -1) || (value.indexOf(QUOTE) != -1) || (value.indexOf('\n') != -1) // line break
                || value.startsWith(" ") || value.endsWith(" ") //$NON-NLS-1$ //$NON-NLS-2$
                || value.startsWith("\t") || value.endsWith("\t"); //$NON-NLS-1$ //$NON-NLS-2$
        if (needQuote) {
            value = QUOTE + value + QUOTE;
        }

        return value;
    }
}

Related

  1. quoteComma(String value)
  2. quoteCommandArgument(String s)
  3. quoteComments(String value)
  4. quoteContentsOfNextBracket(String piece)
  5. quoteCSV(Object val)
  6. quoted(CharSequence s)
  7. quoted(CharSequence s)
  8. quoted(final String str)
  9. quoted(String i, char q)