Java String Tokenize tokenize(String formula)

Here you can find the source of tokenize(String formula)

Description

tokenize

License

Open Source License

Declaration

public static List<String> tokenize(String formula) 

Method Source Code

//package com.java2s;
/*//from  ww w .j ava2s. c o m
 * Copyright (C) 2018 Chan Chung Kwong
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;
import java.util.List;

public class Main {
    public static List<String> tokenize(String formula) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < formula.length(); i++) {
            char c = formula.charAt(i);
            if (c == '\\') {
                if (i + 1 < formula.length()) {
                    c = formula.charAt(i + 1);
                    if ((c >= 'a' && c <= 'z') || (c >= 'a' && c <= 'z')) {
                        int j = i + 2;
                        while (j < formula.length()) {
                            c = formula.charAt(j);
                            if ((c >= 'a' && c <= 'z') || (c >= 'a' && c <= 'z')) {
                                ++j;
                            } else {
                                break;
                            }
                        }
                        if (formula.substring(i, j).equals("\\begin") || formula.substring(i, j).equals("\\end")) {
                            int k = formula.indexOf('}', j);
                            j = (k >= 0 ? k + 1 : formula.length());
                        }
                        list.add(formula.substring(i, j));
                        i = j - 1;
                    } else {
                        list.add(formula.substring(i, i + 2));
                        ++i;
                    }
                }
            } else if (c == '%') {
                int j = formula.indexOf('\n', i);
                if (j >= 0) {
                    i = j;
                } else {
                    j = formula.indexOf('\r', i);
                    if (j >= 0) {
                        i = j;
                    } else {
                        break;
                    }
                }
            } else if (!Character.isWhitespace(c)) {
                list.add(formula.substring(i, i + 1));
            }
        }
        return list;
    }
}

Related

  1. substringsBetween(final String str, final String open, final String close, boolean tokenReservedFlag)
  2. tokenArray(StringTokenizer st)
  3. tokenize(final String aInput, final String aDelimiters)
  4. tokenize(final String s)
  5. tokenize(String array, String delimiter)
  6. tokenize(String in)
  7. tokenize(String input)
  8. tokenize(String input)
  9. tokenize(String input)