Java CSV String Convert csvEncodeString(String value)

Here you can find the source of csvEncodeString(String value)

Description

This function is used when you build a comma separated list of filter values where you can't use an Object [].

License

Apache License

Parameter

Parameter Description
value String value to escape commas in

Declaration

public static String csvEncodeString(String value) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  .ja  v a 2s  .com
 * Copyright 2015 Apothem.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * This function is used when you build a comma separated list of filter values where you can't use an Object [].  Passing an Object []
     * is the preferred way to pass values to filters if you are going to use a list.  But if you are forced, you can use a CSV format.  
     * Use this function to ensure that commas in normal strings are escaped.
     * for example:
     * String list = FilterHelper.csvEncodeString(val1) + "," FilterHelper.csvEncodeString(val2)
     * filters.add("name", "in", list);
     * @param value String value to escape commas in
     * @return
     */
    public static String csvEncodeString(String value) {
        return value.replaceAll("(?!\\\\),", "\\\\,");
    }
}

Related

  1. csvDecodeString(String value)
  2. csvDelimiter(String line)
  3. csvEncode(int value)
  4. csvQuote(String value)
  5. csvString(Object[] objects)
  6. csvstring(Object[] str)
  7. csvTitleToTag(String title, int index)