Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2007-2010 the original author or authors.
 * 
 * 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.
 */

import java.util.LinkedList;
import java.util.List;

public class Main {
    private static final String[] EMPTY_STRING = new String[0];

    static String[] tokenize(String listString, String delimiters) {
        char[] delimiterChars = delimiters.toCharArray();

        char[] listChars = listString.toCharArray();
        int i = 0;

        List<String> stringList = new LinkedList<String>();

        StringBuffer buffer = new StringBuffer();
        while (i < listChars.length) {

            char c = listChars[i];

            boolean advance = false;
            for (char d : delimiterChars) {
                if (c == d) {
                    stringList.add(buffer.toString());
                    buffer = new StringBuffer();
                    advance = true;
                    break;
                }
            }

            if (!advance) {
                if (c == '\\') {
                    if (i < listChars.length - 1) {
                        char next = listChars[i + 1];

                        //if one of next chars is delimiter chars, 
                        //then advance to next char
                        for (char d : delimiterChars) {
                            if (d == next) {
                                i++;
                                c = next;
                                break;
                            }
                        }
                    }
                }

                buffer.append(c);
            }

            i++;
        }

        //add last buffer to String
        stringList.add(buffer.toString());

        String[] pairings = stringList.toArray(EMPTY_STRING);
        //StringUtils.tokenizeToStringArray(listString, delimiters);
        return pairings;
    }
}