Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String difference(String xpath1, String xpath2) {
        String[] paths1 = split(xpath1);
        String[] paths2 = split(xpath2);
        int length = Math.min(paths1.length, paths2.length);
        int index = 0;
        while (index < length && paths1[index].equals(paths2[index])) {
            index++;
        }
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < index; i++) {
            b.append(paths1[i]);
        }
        while (index < paths1.length) {
            b.append(paths1[index++]);
            b.append('/');
        }
        return b.toString();
    }

    public static String[] split(String xpath) {
        String[] paths = xpath.split("/");
        if (paths.length == 0) {
            paths = new String[1];
            paths[0] = "";
        }
        return paths;
    }
}