简体   繁体   English

Java-检查字符串之间的空间

[英]Java - check space between string

I developed a compare text tool using Java Swing. 我使用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. 我所做的是,我基本上用" "(space) split了字符串,然后添加到我的数组列表中。 The arraylist will look like this. arraylist将如下所示。

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. 然后,我将array1中的第一个元素与array2中的第一个元素进行比较。 If it equals the compare is PASS and if it fails I need to show as FAIL . 如果等于,则比较为PASS ,如果失败,则需要显示为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] [您好,World,,Java]

String B = "Hello World Java"

output: 输出:

[Hello, World, Java] [您好,世界,Java]

so, to solve this, i use the replace to clean the final output, like: 因此,要解决此问题,我可以使用replace来清理最终输出,例如:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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