mardi 8 mai 2018

Shorten if statements with .contains in them

I was working on a little program and i wanted to ask if there was a different way to go about the if else soup im having. Every single if and else if have a string.contains comparison in them. Im doing this in JAVA 8.

Is there a way you can shorten this using something like x ? y : z? Or maybe a switch?

if (virtualhostsReading) {
                if (s.contains("ServerAdmin ")) {

                } else if (s.contains("DocumentRoot")) {

                } else if (s.contains("ServerName")) {

                } else if (s.contains("ErrorLog")) {

                } else if (s.contains("CustomLog")) {

                } else if (s.contains("<Directory")) {

                } 
            }

Is there a better way to go about this?

~Thanks in advance for any assistance.

Aucun commentaire:

Enregistrer un commentaire