Prasanta Biswas
Prasanta Biswas

Reputation: 829

How to compare two string with ignoring some substring of the target string

Suppose I have two strings like

String 1: You have transferred balance 1000. Transaction ID:12345670
String 2: You have transferred balance 1000. Transaction ID:@ignore

I want to compare the 2nd string with 1st string with ignoring the dynamic transaction id part with some sort of keyword(example:- @ignore). So while comparing the 2nd string with the 1st string, if it finds the keyword in a place of the target string, it will ignore that specific part in the source string and return true.

Can this be done in Java or are there any java library available for this kind of operation?

Upvotes: 2

Views: 5323

Answers (5)

jai
jai

Reputation: 118

Replace your keyword @ignore with regEx then do comparison.

So you can put @ignore anywhere in your string2 to ignore any part of string1 while comparison.

Ex:

String str2 = You have transferred @ignore 1000. Transaction ID:@ignore""

  String str1 = new String("You have transferred balance 1000. Transaction ID:12345670");
  String str2 = new String("You have transferred balance 1000. Transaction ID:@ignore");

  String regEx =  str2.replace("@ignore", "(.*)");

  System.out.print("Result :" );
  System.out.println(str1.matches(regEx));

Upvotes: 2

aggharta
aggharta

Reputation: 273

In case the @ignore statement is not at the end, you can split the String that contains it and check if the first String starts and ends with the respective parts.

    String b = "You have transferred balance 1000. Transaction ID:12345670 Some more information:1234567";
    String a = "You have transferred balance 1000. Transaction ID:@ignore Some more information:1234567";

    String[] parts = a.split("@ignore");

    System.out.println(b.startsWith(parts[0]) && b.endsWith(parts[parts.length-1]));

This does not cover the case where multiple @ignore tokens are present, but then you would have to replace the respective part of the @ignore in the first string and therefore need some more information about it, e.g. length or tokens for start and stop.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

Simply convert the template to a regex pattern.

String s1 ="You have transferred balance 1000, Transaction ID:12345670";
String s2 = "You have transferred balance 1000, Transaction ID:@ignore";

System.out.println(matches(s1, s2));

boolean matches(String text, String template) {
    StringBuilder sb = new StringBuilder("(?s)"); // Dot also for line breaks.
    for (int i = 0; i < template.length(); ++i) {
        int j = template.indexOf("@ignore", i);
        if (j == -1) {
            // Escape (quote) the regex special chars in as-is text.
            sb.append(Pattern.quote(template.substring(i));
            break;
        }
        sb.append(Pattern.quote(template.substring(i, j));
        j += "@ignore".length();
        sb.append(".*?"); // Shortest sequence.
    }
    return text.matches(sb.toString());
}

Mind: the pattern matches the entire text, hence a "find" should be done as "(?s).*" + actualPattern + ".*".

Upvotes: 0

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

Use regular expression instead of the second string. This can be done with String.matches:

String string = "You have transferred balance 1000. Transaction ID:12345670";
if (string.matches(
  "You have transferred balance 1000. Transaction ID:\\d+")) { ...

Upvotes: 0

AssenKhan
AssenKhan

Reputation: 566

try this below one

        final String s1 ="You have transferred balance 1000, Transaction ID:12345670";
        final String s2 = "You have transferred balance 1000, Transaction ID:@ignore";

        final String[] s1Split = s1.split(":");
        final String[] s2Split = s1.split(":");

        System.out.println(s1Split[0].equals(s2Split[0]));

Upvotes: 2

Related Questions