I am assuming that if the two strings have the same length, any of them will be OK as the longer.
I am finding the length by counting the code points in the string. In most cases this will count the characters, unlike using String.length()
. More discussion of defining the length of a string below.
Collections.max()
and a Comparator
As Anonymous said in a comment:
private static final Comparator<String> compareByLengthInCodePoints
= Comparator.comparingInt(s -> s.codePointCount(0, s.length()));
public static void printLonger(String s1, String s2) {
String longer = Collections.max(List.of(s1, s2), compareByLengthInCodePoints);
System.out.println(longer + " length " + longer.codePointCount(0, longer.length()));
}
Try it out:
String threeLetters = "abc";
String twoSmileys = "\uD83D\uDE42\uD83D\uDE20";
printLonger(threeLetters, twoSmileys);
Output:
abc length 3
As another solution we can use a while
or for
loop to simulate an if
statement. This auxiliary method does that:
private static String getLongerString(String s1, String s2) {
while (s1.codePointCount(0, s1.length()) > s2.codePointCount(0, s2.length())) {
return s1;
}
return s2;
}
Just call it from the printLonger()
method from before. My IDE warns me: 'while' statement does not loop. Which is exactly point.
The Java char
type is virtually deprecated since is does can no longer represent a Unicode character. By extension also the String.length()
method no longer gives a meaningful value since it gives the number of char
s in the string.
A Java char
is 16 bits, so there are 65 000 possible char
values. Last time I checked, there were 292 000 different Unicode characters, and the number is still growing. So nearly 3 times as many. Therefore some characters use two Java char
s, for example smileys.
A character is therefore represented by a 32 bits code point, or by two consecutive char
s in a string.
Counting code points still isn’t perfect. Some code points represent non-printing characters, for example change of reading direction or accents or other diacritical marks.
Yet other thinkable definitions of the length of a string would be the width in pixels when rendering the string using some specific font.