3

Ok, I'm sure that this must exist on here somewhere, but I can't seem to find it.

Is there, and if there is what is, a minimum (non-null) String sequence according to String#CompareTo?

I'm guessing "" but I'm not entirely sure.

Jeff
  • 12,555
  • 5
  • 33
  • 60

5 Answers5

1

Given the implementation of String#compareTo (see source) you can verify that a string of size 0 (thus the empty string) will always be inferior or equal to any other non null string. (equal in case of comparing to the empty string).

benzonico
  • 10,635
  • 5
  • 42
  • 50
1

Edit:

Try this,

   String s="";
      String s1=new String(new char[-2]);  // Here you will get NagativeArraySize Exception
     System.out.println(s1.compareTo(s));

Here compareTo returns nothing in System.out.println(), so the minimum string will be "" or String.Empty


AFAIK, There is no minimum lenght and you can not find a string with the length <0, so the minimum length for the string 0 (string.Empty or "").

Check this source code of CompareTo ,

public int compareTo(String anotherString) {
        int len1 = count;       // original string count
        int len2 = anotherString.count;  // comparision string count
        int n = Math.min(len1, len2);       
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;

        if (i == j) {
            int k = i;
            int lim = n + i;
            while (k < lim) {
                char c1 = v1[k];
                char c2 = v2[k];
                if (c1 != c2) {
                    return c1 - c2;
                }
                k++;
            }
        } else {
            while (n-- != 0) {
                char c1 = v1[i++];
                char c2 = v2[j++];
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
        return len1 - len2;
    }
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • I wasn't asking for the minimum length, but +1 for the code that answers the question anyway. – Jeff Mar 23 '13 at 21:30
0

There is no minimum length, since you can validly compare two empty strings with "".compareTo("")

HXCaine
  • 4,228
  • 3
  • 31
  • 36
  • 2
    Well, the length can't be less than 0 so "" is effectively the minimum length, no? – ddmps Mar 23 '13 at 21:04
  • 1
    Yes, I assumed that was implicit. I.e. that there is no minimum length sequence for this particular operation other than the minimum length of a String – HXCaine Mar 23 '13 at 21:08
  • 1
    This doesn't answer the question. I wasn't asking for minimum length, I was asking for the minimum String as per lexicographic ordering. – Jeff Mar 23 '13 at 21:26
  • I see, I must have misunderstood. Perhaps you could say that in the question – HXCaine Mar 23 '13 at 23:53
0

Yes it is a empty String with not values in it.

Take a look at this sample code :

String str = "";
str.compareTo("");
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • 1
    While this apparently has the right answer, the example doesn't really prove that... – Jeff Mar 23 '13 at 21:27
0

Yes the minimum length string will be an empty string "" with a length of 0. I don't think there will be such a string with length less than 0.

user_CC
  • 4,686
  • 3
  • 20
  • 15
  • 1
    This doesn't answer the question. I wasn't asking for minimum length, I was asking for the minimum String as per lexicographic ordering. – Jeff Mar 23 '13 at 21:26