Java String Split by Quote splitQuotedStr(String str, char delimiter, char quote, boolean trim)

Here you can find the source of splitQuotedStr(String str, char delimiter, char quote, boolean trim)

Description

split Quoted Str

License

Open Source License

Declaration

public static List<String> splitQuotedStr(String str, char delimiter, char quote, boolean trim) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
 * 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
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> splitQuotedStr(String str, char delimiter, char quote, boolean trim) {
        int startPos = 0;
        boolean insideQuotation = false;
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == quote) {
                insideQuotation = !insideQuotation;
            }//from   ww w .  j  a  v a  2 s.c o  m
            if (!insideQuotation && c == delimiter) {
                String subStr = str.substring(startPos, i);
                if (trim) {
                    subStr = subStr.trim();
                }
                result.add(subStr);
                startPos = i + 1;
                continue;
            }
        }
        if (startPos < str.length()) {
            String subStr = str.substring(startPos, str.length());
            if (trim) {
                subStr = subStr.trim();
            }
            result.add(subStr);
        }
        return result;
    }
}

Related

  1. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  2. splitOptions(String quotedOptionString)
  3. splitQuoted(String input, Character split)
  4. splitQuoted(String str, char delimiter)
  5. splitQuotedEscape(String src)
  6. splitQuotedString(String str)
  7. splitQuotedString(String str)
  8. splitQuotedTokens(String str)
  9. splitSafeQuote(String input, char separator)