Is there a quick way to select the longer of two strings? I want to circumvent having to do
if(string1 > string2)
do a;
else if(string2 > string1)
do b;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
String have a method
lengthyou can use:Edit: I misunderstood your question. This would be a shorter way to write it using a ternary operator:
I don’t know why you are looking for a shorter way to write it since your original way gives much more readability than using a ternary operator. If the reason is that you’re going to get the longest string many times I’d recommend to extract the comparison to a method that returns the longest string instead. E.g: