简体   繁体   中英

Java - check space between string

I developed a compare text tool using Java Swing. My comparison login works like this.

String A = "Hello World  Java"
String B = "Hello World Java"

What i did is, I basically split the string use " "(space) then add into my array list. The arraylist will look like this.

array1(0) = {Hello} array1(1)={World} array1(2)={Java}
array2(0) = {Hello} array2(1)={World} array2(2)={Java}

Then I compare using the first element in array1 with first element in array2. If it equals the compare is PASS and if it fails I need to show as FAIL . And the out put will be like this.

<pass>Hello<pass><pass>World<pass><pass>Java<pass>  

I have an enhancement where I need to show the spacing as well. So my final output need to be like this.

<pass>Hello<pass><quote><pass>World<pass><quote><quote><pass>Java<pass>

Any idea how I can achieve this? Please advice.

Try to replace parts of original string with elements from array, where elements would be "enhanced" with information of pass or fail.

void checkString(String orig, String part, Boolean passed) {
    if (passed) {
        a.replace(part, "<pass>" + part + "<pass>");
    } else {
        a.replace(part, "<fail>" + part + "<fail>");
    }
}

So your example:

String A = "Hello World  Java"
String B = "Hello World Java"

// Do comparison, like you do

// take your first array
array1(0) = {Hello} array1(1)={World} array1(2)={Java}

// call method for each part in array
checkString(A, array1(0), isPassed);

I tried to run some simulation of this, and i have this results:

String A = "Hello World  Java"

output:

[Hello, World, , Java]

String B = "Hello World Java"

output:

[Hello, World, Java]

so, to solve this, i use the replace to clean the final output, like:

Arrays.toString(yourString.split(" ")).replace(" ", "").replace(",,", ",")

I think it will work with every input.

Hope it helps ^^

Is this you want?

public static String compare(String[] a, String[] b) {
    String op = "";
    int len = a.length > b.length ? a.length : b.length;
    for (int i = 0; i < len; i++) {
        String aVal = "";
        String bVal = "";
        if (a.length > i) {
            aVal = a[i];
        }
        if (b.length > i) {
            bVal = b[i];
        }
        if (aVal.equals(bVal)) {
            op += "<pass>" + a[i] + "<pass>";
        } else {
            op += "<fail>" + a[i] + "<fail>";
        }
        if (i != len - 1) {
            op += "<quote>";
        }
    }
    return op;
}

public static void main(String[] args) {
    String A = "Hello World Java1 A";
    String B = "Hello World Java";
    String[] splitA = A.split(" ");
    String[] splitB = B.split(" ");
    System.out.println(compare(splitA, splitB));
}

Output:

<pass>Hello<pass><quote><pass>World<pass><quote><fail>Java1<fail><quote><fail>A<fail>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM