Replace part of a String in Java

4

I have Strings that repeat themselves, just changing a word. I have a case to mount the email content, but I do not want to repeat the entire string, and the only value that will be different is whether it is front-end, backend, mobile, etc.

How do I do this in the best way? For 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;
        }
    
asked by anonymous 19.09.2017 / 05:06

3 answers

5

You can use the class / method "String.format ()", which allows you to reuse the string, 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 );

Output

  

As soon as we have the opportunity for a frontend programmer, we'll get in touch.

This might be interesting: link

    
19.09.2017 / 05:49
3

You can use a HashMap to store all the professions and use the aforementioned method to create the desired String. As 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: link

This way the code becomes much easier to maintain, since it prevents you from having to make a huge control structure. So if you need to add a new profession, you just need to make a new put in your HashMap .

    
19.09.2017 / 06:59
1

The above answers are good, but I'll leave a possible solution too:

You can use the StringBuilder class, which is just for working 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 into a String "pure", just call the toString() method.

In addition, the code may get a bit simpler because the StringBuilder class has a constructor that receives a CharSequence as a parameter (class String implements 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

    
19.09.2017 / 18:52