Secret Coder Secret Coder - 4 months ago 8
Android Question

Checking of String if it has this specific value

So i have this problem. I have a string token which look like this "jdK10ds3" and sometimes with "jdK10ds3TEST". I want to check if the string has "TEST" starting to the last. I tried using

but it can detect the string when it is on the middle like this "jdK1TESTds3".

How to check the string if it has "TEST" on the last(ignoring the cases) and how to parse the string to remove the TEST on the last. It is possible that the "TEST" can be doubled like this "jdK1TESTds3TEST"


Regex is your friend :

public static void main(String[] args) throws Exception {
    String s1 = "jdK1TESTds3TEST";
    System.out.println(s1.replaceAll("(?i)(.*)TEST$", "$1"));

O/P : jdK1TESTds3

The code above replaces the last TEST (if it exists). Add (?i) modifier to make it case-insenstive.