Java Collection Convert toEscapedStringWithDelimiters(Collection objects, String delim)

Here you can find the source of toEscapedStringWithDelimiters(Collection objects, String delim)

Description

to Escaped String With Delimiters

License

LGPL

Declaration

public static String toEscapedStringWithDelimiters(Collection objects, String delim) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    public static String toEscapedStringWithDelimiters(Collection objects, String delim) {
        StringBuffer buffer = new StringBuffer();
        int index = 0;
        for (Object obj : objects) {
            if (index++ > 0)
                buffer.append(delim);/*ww  w .  jav a 2  s.c  o  m*/
            String string = obj.toString();
            if (string.startsWith("\"") && string.endsWith("\"")) {
                if (string.length() <= 2)
                    string = "\" \" ";
                else
                    string = "\"" + replaceQuotationMarks(string.substring(1, string.length() - 1)) + "\"";
            }
            buffer.append(string);
        }
        return buffer.toString();
    }

    public static String replaceQuotationMarks(String s) {
        if (s.contains("\"")) {
            return s.replaceAll("[\"]", "''");
        }
        return s;
    }
}

Related

  1. toDelimitedString(Collection c, String delimiter)
  2. toDelimitedString(Collection coll, String delim)
  3. toDelimitedString(final Collection coll, final String delim)
  4. toDoubles(Collection numbers)
  5. toEnumeration(final Collection collection)
  6. toggleEntry(T entry, Collection collection)
  7. toHashSet(Collection c)
  8. toHtml(Collection collection)
  9. toIntegerCollection(int[] ints)