src/share/classes/java/lang/String.java

Print this page




 993      * is {@code true} if and only if this {@code String} represents the same
 994      * sequence of characters as the specified {@code StringBuffer}. This method
 995      * synchronizes on the {@code StringBuffer}.
 996      *
 997      * @param  sb
 998      *         The {@code StringBuffer} to compare this {@code String} against
 999      *
1000      * @return  {@code true} if this {@code String} represents the same
1001      *          sequence of characters as the specified {@code StringBuffer},
1002      *          {@code false} otherwise
1003      *
1004      * @since  1.4
1005      */
1006     public boolean contentEquals(StringBuffer sb) {
1007         return contentEquals((CharSequence)sb);
1008     }
1009 
1010     private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1011         char v1[] = value;
1012         char v2[] = sb.getValue();
1013         int i = 0;
1014         int n = value.length;
1015         while (n-- != 0) {


1016             if (v1[i] != v2[i]) {
1017                 return false;
1018             }
1019             i++;
1020         }
1021         return true;
1022     }
1023 
1024     /**
1025      * Compares this string to the specified {@code CharSequence}.  The
1026      * result is {@code true} if and only if this {@code String} represents the
1027      * same sequence of char values as the specified sequence. Note that if the
1028      * {@code CharSequence} is a {@code StringBuffer} then the method
1029      * synchronizes on it.
1030      *
1031      * @param  cs
1032      *         The sequence to compare this {@code String} against
1033      *
1034      * @return  {@code true} if this {@code String} represents the same
1035      *          sequence of char values as the specified sequence, {@code
1036      *          false} otherwise
1037      *
1038      * @since  1.5
1039      */
1040     public boolean contentEquals(CharSequence cs) {
1041         if (value.length != cs.length())
1042             return false;
1043         // Argument is a StringBuffer, StringBuilder
1044         if (cs instanceof AbstractStringBuilder) {
1045             if (cs instanceof StringBuffer) {
1046                 synchronized(cs) {
1047                    return nonSyncContentEquals((AbstractStringBuilder)cs);
1048                 }
1049             } else {
1050                 return nonSyncContentEquals((AbstractStringBuilder)cs);
1051             }
1052         }
1053         // Argument is a String
1054         if (cs.equals(this))
1055             return true;
1056         // Argument is a generic CharSequence
1057         char v1[] = value;
1058         int i = 0;
1059         int n = value.length;
1060         while (n-- != 0) {
1061             if (v1[i] != cs.charAt(i))
1062                 return false;
1063             i++;



1064         }
1065         return true;
1066     }
1067 
1068     /**
1069      * Compares this {@code String} to another {@code String}, ignoring case
1070      * considerations.  Two strings are considered equal ignoring case if they
1071      * are of the same length and corresponding characters in the two strings
1072      * are equal ignoring case.
1073      *
1074      * <p> Two characters {@code c1} and {@code c2} are considered the same
1075      * ignoring case if at least one of the following is true:
1076      * <ul>
1077      *   <li> The two characters are the same (as compared by the
1078      *        {@code ==} operator)
1079      *   <li> Applying the method {@link
1080      *        java.lang.Character#toUpperCase(char)} to each character
1081      *        produces the same result
1082      *   <li> Applying the method {@link
1083      *        java.lang.Character#toLowerCase(char)} to each character




 993      * is {@code true} if and only if this {@code String} represents the same
 994      * sequence of characters as the specified {@code StringBuffer}. This method
 995      * synchronizes on the {@code StringBuffer}.
 996      *
 997      * @param  sb
 998      *         The {@code StringBuffer} to compare this {@code String} against
 999      *
1000      * @return  {@code true} if this {@code String} represents the same
1001      *          sequence of characters as the specified {@code StringBuffer},
1002      *          {@code false} otherwise
1003      *
1004      * @since  1.4
1005      */
1006     public boolean contentEquals(StringBuffer sb) {
1007         return contentEquals((CharSequence)sb);
1008     }
1009 
1010     private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1011         char v1[] = value;
1012         char v2[] = sb.getValue();
1013         int n = v1.length;
1014         if (n != v2.length) {
1015             return false;
1016         }
1017         for (int i = 0; i < n; i++) {
1018             if (v1[i] != v2[i]) {
1019                 return false;
1020             }

1021         }
1022         return true;
1023     }
1024 
1025     /**
1026      * Compares this string to the specified {@code CharSequence}.  The
1027      * result is {@code true} if and only if this {@code String} represents the
1028      * same sequence of char values as the specified sequence. Note that if the
1029      * {@code CharSequence} is a {@code StringBuffer} then the method
1030      * synchronizes on it.
1031      *
1032      * @param  cs
1033      *         The sequence to compare this {@code String} against
1034      *
1035      * @return  {@code true} if this {@code String} represents the same
1036      *          sequence of char values as the specified sequence, {@code
1037      *          false} otherwise
1038      *
1039      * @since  1.5
1040      */
1041     public boolean contentEquals(CharSequence cs) {


1042         // Argument is a StringBuffer, StringBuilder
1043         if (cs instanceof AbstractStringBuilder) {
1044             if (cs instanceof StringBuffer) {
1045                 synchronized(cs) {
1046                    return nonSyncContentEquals((AbstractStringBuilder)cs);
1047                 }
1048             } else {
1049                 return nonSyncContentEquals((AbstractStringBuilder)cs);
1050             }
1051         }
1052         // Argument is a String
1053         if (cs.equals(this))
1054             return true;
1055         // Argument is a generic CharSequence
1056         char v1[] = value;
1057         int n = v1.length;
1058         if (n != cs.length())


1059             return false;
1060         for (int i = 0; i < n; i++) {
1061             if (v1[i] != cs.charAt(i)) {
1062                 return false;
1063             }
1064         }
1065         return true;
1066     }
1067 
1068     /**
1069      * Compares this {@code String} to another {@code String}, ignoring case
1070      * considerations.  Two strings are considered equal ignoring case if they
1071      * are of the same length and corresponding characters in the two strings
1072      * are equal ignoring case.
1073      *
1074      * <p> Two characters {@code c1} and {@code c2} are considered the same
1075      * ignoring case if at least one of the following is true:
1076      * <ul>
1077      *   <li> The two characters are the same (as compared by the
1078      *        {@code ==} operator)
1079      *   <li> Applying the method {@link
1080      *        java.lang.Character#toUpperCase(char)} to each character
1081      *        produces the same result
1082      *   <li> Applying the method {@link
1083      *        java.lang.Character#toLowerCase(char)} to each character