Java Scanner Usage removeIndexes(String xpath)

Here you can find the source of removeIndexes(String xpath)

Description

Remove indexes from an xpath string.

License

Open Source License

Parameter

Parameter Description
xpath xpath string

Return

unindexed xpath string

Declaration

public static String removeIndexes(String xpath) 

Method Source Code

//package com.java2s;
/*// w ww .java2  s.  c o m
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2009-2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

import java.util.Scanner;

public class Main {
    /**
     * Remove indexes from an xpath string.
     * @param xpath xpath string
     * @return unindexed xpath string
     */
    public static String removeIndexes(String xpath) {
        final String[] partialSteps = xpath.split("[/]");
        if (partialSteps.length == 0) {
            return xpath;
        }

        int startIndex = 0;
        StringBuffer buf = new StringBuffer();

        for (int i = startIndex; i < partialSteps.length; i++) {
            String step = partialSteps[i];
            int start = step.indexOf('[');

            if (start > -1) {
                int end = step.indexOf(']');
                Scanner scanner = new Scanner(step.substring(start + 1, end));
                if (scanner.hasNextInt()) {
                    // remove index and the brackets
                    step = step.substring(0, start);
                }
            }
            buf.append(step);
            if (i < partialSteps.length - 1) {
                buf.append("/");
            }
        }
        return buf.toString();
    }
}

Related

  1. pressEnterToContinue()
  2. processLine(String aLine)
  3. queryMenu(Scanner in, String msg, LinkedList options)
  4. queryStr(Scanner in, String msg)
  5. removeFirstNLines(String text, int n)
  6. removeNLinesForwardsFromStringMatch(String text, String match, int n)
  7. selectProject(List projectList)
  8. splitLines(String string)
  9. splitToDouble(String input)