Java tutorial
//package com.java2s; public class Main { /** * @param tag e.g., tr * @param xpath e.g., /html/body/table[1]/tr[2]/td[2] * @return /html/body/table[1]/tr[2] */ public static String trimXPathLastTag(String tag, String xpath) { int lastTagIndex = xpath.lastIndexOf(tag); if (lastTagIndex != -1) { int end = xpath.indexOf("/", lastTagIndex); if (end == -1) { return xpath; } return xpath.substring(0, end); } return null; } }