Replace part of a Java String

Asked

Viewed 430 times

4

I have repeating Strings, just changing a word. I have one case to assemble email content, but do not want to keep repeating the whole string, being that the only value that will be different, is whether it is front-end, back-end, mobile, etc.

How do I do it the best way? To get a cleaner code?

String mailContent = "";
        switch (mail) {
        case "FRONTEND":
            mailContent = "Assim que tivermos oportunidade para programador front-end, entraremos em contato.";
            break;

        case "BACKEND":
            mailContent = "Assim que tivermos oportunidade para programador back-end, entraremos em contato.";
            break;

        case "MOBILE":
            mailContent = "Assim que tivermos oportunidade para programador mobile, entraremos em contato.";
            break;

        case "GENERIC":
            mailContent = "Assim que tivermos oportunidade para programador, entraremos em contato.";
            break;

        default:
            break;
        }
  • 1

    It’s not enough to make a if and a toLowerCase() ?

3 answers

5

You can use the "String.format()" class/method, which allows you to reuse the string by passing the variables you want to replace.

String cargo = "FRONTEND";
String mailContent = String.format("Assim que tivermos oportunidade para programador %s, entraremos em contato.", cargo.toLowerCase());
System.out.println(mailContent );

Exit

As soon as we have opportunity for frontend programmer, we will contact.

This could be interesting: https://dzone.com/articles/java-string-format-examples

3

You can use a Hashmap to store all the professions and use the method already cited to create the desired String. As follows in the example below:

Map<String, String> occupations = new HashMap<String, String>();

occupations.put("FRONTEND", new String("programador front-end"));
occupations.put("BACKEND", new String("programador back-end"));
occupations.put("MOBILE", new String("programador mobile"));
occupations.put("GENERIC", new String("programador"));

String profession = occupations.get("MOBILE");

String mailContent = String.format("Assim que tivermos oportunidade para %s, entraremos em contato.", profession);
System.out.println(mailContent);

Full example: https://ideone.com/VpmbyF

This way the code is much easier to maintain, because it prevents you from having to make a huge control structure. So if you need to add a new profession, you only need to do a new one put in his Hashmap.

1

The previous answers are good, but I’ll leave a possible solution too:

You can use the class StringBuilder, which serves precisely to work with dynamic strings.

StringBuilder mailContent = new StringBuilder();
mailContent.append("Assim que tivermos oportunidade para programador");

switch (mail) {
case "FRONTEND":
    mailContent.append(" front-end");
    break;
case "BACKEND":
    mailContent.append(" back-end");
    break;
case "MOBILE":
    mailContent.append(" mobile");
    break;
}

mailContent.append(", entraremos em contato.");

return mailContent.toString();

Note that to transform the value of a StringBuilder in a String "pure", just call the method toString().

Also, the code can get a little simpler once the class StringBuilder has a builder who receives a CharSequence as a parameter (the class String implements the interface CharSequence):

StringBuilder mailContent = new StringBuilder("Assim que tivermos oportunidade para programador");

switch (mail) {
case "FRONTEND":
    mailContent.append(" front-end");
    break;
case "BACKEND":
    mailContent.append(" back-end");
    break;
case "MOBILE":
    mailContent.append(" mobile");
    break;
}

mailContent.append(", entraremos em contato.");

return mailContent.toString();

Stringbuilder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.