0

My address as string contains this details

String address = "S/O: KANTH MEIPPEN PACHU, N-2345, THAMBULI";

Here masking to be implemented for the above mentioned address line in below format

if the address line starts with S/O, D/O, W/O, C/O it should mask address line like

S/O: KA******************************BULI

ie,. after any of mentioned string masking to be done on the address leaving first four and last four chars

We can use startsWith method but we cannot pass array inside it to check with address string also how to configure the S/O, D/O, W/O, C/O in a single constant with separator to check as it will reduce the code mess up

Here is the java code:

public static void main(String ars[]) throws Exception {
    String address = "S/O: KANTH MEIPPEN PACHU, N-2345, THAMBULI;
    String[] words = {
        "S/O",
        "D/O",
        "W/O",
        "C/O"
    };
    String joined = Arrays.toString(words);
    //list.stream().filter(p -> p.getName().equals(name)).findFirst().isPresent();
    //  ArrayList<String> list = new ArrayList<>(Arrays.asList(address.split("|")));
    System.out.println("Address:::: " + joined);
    //list.stream().anyMatch((a) -> a.startsWith(words));
    if (address.startsWith("S/O") {
            String separator = "S/O";
            int sep = address.indexOf(separator);
            String adress = address.substring(sep + separator.length());
            System.out.println("Address:::: " + adress);
            String masks = String.join("", Collections.nCopies(adress.length() - 8, "*"));
            System.out.println("Mask::: " + masks);
            adress = separator + adress.substring(0, 4) + masks + adress.substring(adress.length() - 4);
        }
    }
Progman
  • 16,827
  • 6
  • 33
  • 48
  • 1
    Tip: always include the programming language as one of the tags on your question (in this case I guess Java?) It's one of the main way people filter the site for questions they're likely to be able to help with, and much more useful than extremely specific tags like "startswith". – IMSoP Jul 16 '23 at 07:51
  • This is strange code, as it checks that S/O is at the start of the string, then finds its position. You know that - it's zero, as it's the start of the string! Suggestions - use regex to match the prefixes, or pass all prefixes in one string separated by colon (say), split them in the function to an array, loop over the array. – The Archetypal Paul Jul 16 '23 at 07:57

2 Answers2

0

I would use something like this :

String[] words = "^[SDWC]/O";

Pattern pattern = Pattern.compile(words);
Matcher m = pattern.matcher(address);

then m.find() returns true or false if the pattern matches.

mgillett
  • 74
  • 8
0

I have updated your code to use Regex

public static void main(String[] args) {
        String address = "S/O: KANTH MEIPPEN PACHU, N-2345, THAMBULI";
        String salutation = "^[SDWC][//]O";
        Pattern pattern = Pattern.compile(salutation);
        Matcher matcher = pattern.matcher(address);
        if (matcher.find()) {
            String group = matcher.group();
            int sep = address.indexOf(group);
            String addressStr = address.substring(sep + group.length());
            System.out.println("Address:::: " + addressStr);
            String masks = String.join("", Collections.nCopies(addressStr.length() - 8, "*"));
            System.out.println("Mask::: " + masks);
            addressStr = group + addressStr.substring(0, 4) + masks + addressStr.substring(addressStr.length() - 4);
            System.out.println(addressStr);
        }
    }
  • this is fine for single string search like we are searching only for S/O, my question is what if sometimes it may be D/O, C/O comes, in that case how to add validation to check for other scenarios too. – Priyadharshini Jul 16 '23 at 08:35
  • It is actually not for a single search, the regex "^[SDWC][//]O" covers everything, it will handle S/O, D/O, W/O and C/O – Manvendra_0611 Jul 16 '23 at 10:53