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

Print this page




2532      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2533      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2534      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2535      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2536      *       <img src="doc-files/sigma1.gif" alt="sigma"></td>
2537      *   <td>lowercased all chars in String</td>
2538      * </tr>
2539      * </table>
2540      *
2541      * @param locale use the case transformation rules for this locale
2542      * @return the {@code String}, converted to lowercase.
2543      * @see     java.lang.String#toLowerCase()
2544      * @see     java.lang.String#toUpperCase()
2545      * @see     java.lang.String#toUpperCase(Locale)
2546      * @since   1.1
2547      */
2548     public String toLowerCase(Locale locale) {
2549         if (locale == null) {
2550             throw new NullPointerException();
2551         }
2552 
2553         int firstUpper;
2554         final int len = value.length;
2555 
2556         /* Now check if there are any characters that need to be changed. */
2557         scan: {
2558             for (firstUpper = 0 ; firstUpper < len; ) {
2559                 char c = value[firstUpper];
2560                 if ((c >= Character.MIN_HIGH_SURROGATE)
2561                         && (c <= Character.MAX_HIGH_SURROGATE)) {
2562                     int supplChar = codePointAt(firstUpper);
2563                     if (supplChar != Character.toLowerCase(supplChar)) {
2564                         break scan;
2565                     }
2566                     firstUpper += Character.charCount(supplChar);
2567                 } else {
2568                     if (c != Character.toLowerCase(c)) {
2569                         break scan;
2570                     }
2571                     firstUpper++;

2572                 }
2573             }

2574             return this;
2575         }
2576 
2577         char[] result = new char[len];
2578         int resultOffset = 0;  /* result may grow, so i+resultOffset
2579                                 * is the write location in result */
2580 
2581         /* Just copy the first few lowerCase characters. */
2582         System.arraycopy(value, 0, result, 0, firstUpper);
2583 
2584         String lang = locale.getLanguage();
2585         boolean localeDependent =
2586                 (lang == "tr" || lang == "az" || lang == "lt");
2587         char[] lowerCharArray;
2588         int lowerChar;
2589         int srcChar;

















2590         int srcCount;
2591         for (int i = firstUpper; i < len; i += srcCount) {
2592             srcChar = (int)value[i];
2593             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
2594                     && (char)srcChar <= Character.MAX_HIGH_SURROGATE) {


2595                 srcChar = codePointAt(i);
2596                 srcCount = Character.charCount(srcChar);
2597             } else {
2598                 srcCount = 1;
2599             }
2600             if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
2601                 lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
2602             } else {
2603                 lowerChar = Character.toLowerCase(srcChar);
2604             }
2605             if ((lowerChar == Character.ERROR)
2606                     || (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {


2607                 if (lowerChar == Character.ERROR) {
2608                     lowerCharArray =
2609                             ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
2610                 } else if (srcCount == 2) {
2611                     resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
2612                     continue;
2613                 } else {
2614                     lowerCharArray = Character.toChars(lowerChar);
2615                 }
2616 
2617                 /* Grow result if needed */
2618                 int mapLen = lowerCharArray.length;
2619                 if (mapLen > srcCount) {
2620                     char[] result2 = new char[result.length + mapLen - srcCount];
2621                     System.arraycopy(result, 0, result2, 0, i + resultOffset);
2622                     result = result2;
2623                 }
2624                 for (int x = 0; x < mapLen; ++x) {
2625                     result[i + resultOffset + x] = lowerCharArray[x];
2626                 }
2627                 resultOffset += (mapLen - srcCount);
2628             } else {
2629                 result[i + resultOffset] = (char)lowerChar;
2630             }
2631         }
2632         return new String(result, 0, len + resultOffset);
2633     }
2634 
2635     /**
2636      * Converts all of the characters in this {@code String} to lower
2637      * case using the rules of the default locale. This is equivalent to calling
2638      * {@code toLowerCase(Locale.getDefault())}.
2639      * <p>
2640      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2641      * results if used for strings that are intended to be interpreted locale
2642      * independently.
2643      * Examples are programming language identifiers, protocol keys, and HTML
2644      * tags.
2645      * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale
2646      * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the
2647      * LATIN SMALL LETTER DOTLESS I character.
2648      * To obtain correct results for locale insensitive strings, use
2649      * {@code toLowerCase(Locale.ROOT)}.
2650      * <p>
2651      * @return  the {@code String}, converted to lowercase.
2652      * @see     java.lang.String#toLowerCase(Locale)


2690      *   <td>small letter sharp s -&gt; two letters: SS</td>
2691      * </tr>
2692      * <tr>
2693      *   <td>(all)</td>
2694      *   <td>Fahrvergn&uuml;gen</td>
2695      *   <td>FAHRVERGN&Uuml;GEN</td>
2696      *   <td></td>
2697      * </tr>
2698      * </table>
2699      * @param locale use the case transformation rules for this locale
2700      * @return the {@code String}, converted to uppercase.
2701      * @see     java.lang.String#toUpperCase()
2702      * @see     java.lang.String#toLowerCase()
2703      * @see     java.lang.String#toLowerCase(Locale)
2704      * @since   1.1
2705      */
2706     public String toUpperCase(Locale locale) {
2707         if (locale == null) {
2708             throw new NullPointerException();
2709         }
2710 
2711         int firstLower;
2712         final int len = value.length;
2713 
2714         /* Now check if there are any characters that need to be changed. */
2715         scan: {
2716             for (firstLower = 0 ; firstLower < len; ) {
2717                 int c = (int)value[firstLower];
2718                 int srcCount;
2719                 if ((c >= Character.MIN_HIGH_SURROGATE)
2720                         && (c <= Character.MAX_HIGH_SURROGATE)) {
2721                     c = codePointAt(firstLower);
2722                     srcCount = Character.charCount(c);
2723                 } else {
2724                     srcCount = 1;
2725                 }
2726                 int upperCaseChar = Character.toUpperCaseEx(c);
2727                 if ((upperCaseChar == Character.ERROR)
2728                         || (c != upperCaseChar)) {
2729                     break scan;
2730                 }
2731                 firstLower += srcCount;
2732             }

2733             return this;
2734         }
2735 
2736         /* result may grow, so i+resultOffset is the write location in result */
2737         int resultOffset = 0;
2738         char[] result = new char[len]; /* may grow */
2739 
2740         /* Just copy the first few upperCase characters. */
2741         System.arraycopy(value, 0, result, 0, firstLower);
2742 
2743         String lang = locale.getLanguage();
2744         boolean localeDependent =
2745                 (lang == "tr" || lang == "az" || lang == "lt");
2746         char[] upperCharArray;
2747         int upperChar;
2748         int srcChar;















2749         int srcCount;
2750         for (int i = firstLower; i < len; i += srcCount) {
2751             srcChar = (int)value[i];
2752             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2753                 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {


2754                 srcChar = codePointAt(i);
2755                 srcCount = Character.charCount(srcChar);
2756             } else {
2757                 srcCount = 1;
2758             }
2759             if (localeDependent) {
2760                 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
2761             } else {
2762                 upperChar = Character.toUpperCaseEx(srcChar);
2763             }
2764             if ((upperChar == Character.ERROR)
2765                     || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {


2766                 if (upperChar == Character.ERROR) {
2767                     if (localeDependent) {
2768                         upperCharArray =
2769                                 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
2770                     } else {
2771                         upperCharArray = Character.toUpperCaseCharArray(srcChar);
2772                     }
2773                 } else if (srcCount == 2) {
2774                     resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
2775                     continue;
2776                 } else {
2777                     upperCharArray = Character.toChars(upperChar);
2778                 }
2779 
2780                 /* Grow result if needed */
2781                 int mapLen = upperCharArray.length;
2782                 if (mapLen > srcCount) {
2783                     char[] result2 = new char[result.length + mapLen - srcCount];
2784                     System.arraycopy(result, 0, result2, 0, i + resultOffset);
2785                     result = result2;
2786                 }
2787                 for (int x = 0; x < mapLen; ++x) {
2788                     result[i + resultOffset + x] = upperCharArray[x];
2789                 }
2790                 resultOffset += (mapLen - srcCount);
2791             } else {
2792                 result[i + resultOffset] = (char)upperChar;
2793             }
2794         }
2795         return new String(result, 0, len + resultOffset);
2796     }
2797 
2798     /**
2799      * Converts all of the characters in this {@code String} to upper
2800      * case using the rules of the default locale. This method is equivalent to
2801      * {@code toUpperCase(Locale.getDefault())}.
2802      * <p>
2803      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2804      * results if used for strings that are intended to be interpreted locale
2805      * independently.
2806      * Examples are programming language identifiers, protocol keys, and HTML
2807      * tags.
2808      * For instance, {@code "title".toUpperCase()} in a Turkish locale
2809      * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
2810      * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2811      * To obtain correct results for locale insensitive strings, use
2812      * {@code toUpperCase(Locale.ROOT)}.
2813      * <p>
2814      * @return  the {@code String}, converted to uppercase.
2815      * @see     java.lang.String#toUpperCase(Locale)




2532      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2533      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2534      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2535      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2536      *       <img src="doc-files/sigma1.gif" alt="sigma"></td>
2537      *   <td>lowercased all chars in String</td>
2538      * </tr>
2539      * </table>
2540      *
2541      * @param locale use the case transformation rules for this locale
2542      * @return the {@code String}, converted to lowercase.
2543      * @see     java.lang.String#toLowerCase()
2544      * @see     java.lang.String#toUpperCase()
2545      * @see     java.lang.String#toUpperCase(Locale)
2546      * @since   1.1
2547      */
2548     public String toLowerCase(Locale locale) {
2549         if (locale == null) {
2550             throw new NullPointerException();
2551         }
2552         int first;
2553         boolean hasSurr = false;
2554         final int len = value.length;
2555 
2556         // Now check if there are any characters that need to be changed, or are surrogate
2557         for (first = 0 ; first < len; first++) {
2558             int cp = (int)value[first];
2559             if (Character.isSurrogate((char)cp)) {
2560                 hasSurr = true;
2561                 break;








2562             }
2563             if (cp != Character.toLowerCase(cp)) {  // no need to check Character.ERROR
2564                 break;
2565             }
2566         }
2567         if (first == len)
2568             return this;


2569         char[] result = new char[len];
2570         System.arraycopy(value, 0, result, 0, first);  // Just copy the first few
2571                                                        // lowerCase characters.




2572         String lang = locale.getLanguage();
2573         if (lang == "tr" || lang == "az" || lang == "lt") {
2574             return toLowerCaseEx(result, first, locale, true);
2575         }
2576         if (hasSurr) {
2577             return toLowerCaseEx(result, first, locale, false);
2578         }
2579         for (int i = first; i < len; i++) {
2580             int cp = (int)value[i];
2581             if (cp == '\u03A3') {                       // GREEK CAPITAL LETTER SIGMA
2582                 return toLowerCaseEx(result, i, locale, false);
2583             }
2584             cp = Character.toLowerCase(cp);
2585             if (!Character.isBmpCodePoint(cp)) {
2586                 return toLowerCaseEx(result, i, locale, false);
2587             }
2588             result[i] = (char)cp;
2589         }
2590         return new String(result, true);
2591     }
2592 
2593     private String toLowerCaseEx(char[] result, int first, Locale locale, boolean localeDependent) {
2594         int resultOffset = first;
2595         int srcCount;
2596         for (int i = first; i < value.length; i += srcCount) {
2597             int srcChar = (int)value[i];
2598             int lowerChar;
2599             char[] lowerCharArray;
2600             srcCount = 1;
2601             if (Character.isSurrogate((char)srcChar)) {
2602                 srcChar = codePointAt(i);
2603                 srcCount = Character.charCount(srcChar);


2604             }
2605             if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
2606                 lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
2607             } else {
2608                 lowerChar = Character.toLowerCase(srcChar);
2609             }
2610             if (lowerChar != Character.ERROR &&
2611                 Character.isBmpCodePoint(lowerChar)) {
2612                 result[resultOffset++] = (char)lowerChar;
2613             } else {
2614                 if (lowerChar == Character.ERROR) {
2615                     lowerCharArray = ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);

2616                 } else if (srcCount == 2) {
2617                     resultOffset += Character.toChars(lowerChar, result, resultOffset);
2618                     continue;
2619                 } else {
2620                     lowerCharArray = Character.toChars(lowerChar);
2621                 }

2622                 /* Grow result if needed */
2623                 int mapLen = lowerCharArray.length;
2624                 if (mapLen > srcCount) {
2625                     char[] result2 = new char[result.length + mapLen - srcCount];
2626                     System.arraycopy(result, 0, result2, 0, resultOffset);
2627                     result = result2;
2628                 }
2629                 for (int x = 0; x < mapLen; ++x) {
2630                     result[resultOffset++] = lowerCharArray[x];
2631                 }



2632             }
2633         }
2634         return new String(result, 0, resultOffset);
2635     }
2636 
2637     /**
2638      * Converts all of the characters in this {@code String} to lower
2639      * case using the rules of the default locale. This is equivalent to calling
2640      * {@code toLowerCase(Locale.getDefault())}.
2641      * <p>
2642      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2643      * results if used for strings that are intended to be interpreted locale
2644      * independently.
2645      * Examples are programming language identifiers, protocol keys, and HTML
2646      * tags.
2647      * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale
2648      * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the
2649      * LATIN SMALL LETTER DOTLESS I character.
2650      * To obtain correct results for locale insensitive strings, use
2651      * {@code toLowerCase(Locale.ROOT)}.
2652      * <p>
2653      * @return  the {@code String}, converted to lowercase.
2654      * @see     java.lang.String#toLowerCase(Locale)


2692      *   <td>small letter sharp s -&gt; two letters: SS</td>
2693      * </tr>
2694      * <tr>
2695      *   <td>(all)</td>
2696      *   <td>Fahrvergn&uuml;gen</td>
2697      *   <td>FAHRVERGN&Uuml;GEN</td>
2698      *   <td></td>
2699      * </tr>
2700      * </table>
2701      * @param locale use the case transformation rules for this locale
2702      * @return the {@code String}, converted to uppercase.
2703      * @see     java.lang.String#toUpperCase()
2704      * @see     java.lang.String#toLowerCase()
2705      * @see     java.lang.String#toLowerCase(Locale)
2706      * @since   1.1
2707      */
2708     public String toUpperCase(Locale locale) {
2709         if (locale == null) {
2710             throw new NullPointerException();
2711         }
2712         int first;
2713         boolean hasSurr = false;
2714         final int len = value.length;
2715 
2716         // Now check if there are any characters that need to be changed, or are surrogate
2717         for (first = 0 ; first < len; first++ ) {
2718             int cp = (int)value[first];
2719             if (Character.isSurrogate((char)cp)) {
2720                 hasSurr = true;
2721                 break;





2722             }
2723             if (cp != Character.toUpperCaseEx(cp)) {   // no need to check Character.ERROR
2724                 break;


2725             }

2726         }
2727         if (first == len) {
2728             return this;
2729         }
2730         char[] result = new char[len];
2731         System.arraycopy(value, 0, result, 0, first);  // Just copy the first few
2732                                                        // upperCase characters.





2733         String lang = locale.getLanguage();
2734         if (lang == "tr" || lang == "az" || lang == "lt") {
2735             return toUpperCaseEx(result, first, locale, true);
2736         }
2737         if (hasSurr) {
2738             return toUpperCaseEx(result, first, locale, false);
2739         }
2740         for (int i = first; i < len; i++) {
2741             int cp = Character.toUpperCaseEx((int)value[i]);
2742             if (cp == Character.ERROR ||
2743                 !Character.isBmpCodePoint(cp)) {
2744                 return toUpperCaseEx(result, i, locale, false);
2745             }
2746             result[i] = (char)cp;
2747         }
2748         return new String(result, true);
2749     }
2750 
2751     private String toUpperCaseEx(char[] result, int first, Locale locale,
2752                                  boolean localeDependent) {
2753         int resultOffset = first;
2754         int srcCount;
2755         for (int i = first; i < value.length; i += srcCount) {
2756             int srcChar = (int)value[i];
2757             int upperChar;
2758             char[] upperCharArray;
2759             srcCount = 1;
2760             if (Character.isSurrogate((char)srcChar)) {
2761                 srcChar = codePointAt(i);
2762                 srcCount = Character.charCount(srcChar);


2763             }
2764             if (localeDependent) {
2765                 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
2766             } else {
2767                 upperChar = Character.toUpperCaseEx(srcChar);
2768             }
2769             if (upperChar != Character.ERROR &&
2770                 Character.isBmpCodePoint(upperChar)) {
2771                 result[resultOffset++] = (char)upperChar;
2772             } else {
2773                 if (upperChar == Character.ERROR) {
2774                     if (localeDependent) {
2775                         upperCharArray =
2776                             ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
2777                     } else {
2778                         upperCharArray = Character.toUpperCaseCharArray(srcChar);
2779                     }
2780                 } else if (srcCount == 2) {
2781                     resultOffset += Character.toChars(upperChar, result, resultOffset);
2782                     continue;
2783                 } else {
2784                     upperCharArray = Character.toChars(upperChar);
2785                 }

2786                 /* Grow result if needed */
2787                 int mapLen = upperCharArray.length;
2788                 if (mapLen > srcCount) {
2789                     char[] result2 = new char[result.length + mapLen - srcCount];
2790                     System.arraycopy(result, 0, result2, 0, resultOffset);
2791                     result = result2;
2792                  }
2793                  for (int x = 0; x < mapLen; ++x) {
2794                     result[resultOffset++] = upperCharArray[x];
2795                  }



2796             }
2797         }
2798         return new String(result, 0, resultOffset);
2799     }
2800 
2801     /**
2802      * Converts all of the characters in this {@code String} to upper
2803      * case using the rules of the default locale. This method is equivalent to
2804      * {@code toUpperCase(Locale.getDefault())}.
2805      * <p>
2806      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2807      * results if used for strings that are intended to be interpreted locale
2808      * independently.
2809      * Examples are programming language identifiers, protocol keys, and HTML
2810      * tags.
2811      * For instance, {@code "title".toUpperCase()} in a Turkish locale
2812      * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
2813      * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2814      * To obtain correct results for locale insensitive strings, use
2815      * {@code toUpperCase(Locale.ROOT)}.
2816      * <p>
2817      * @return  the {@code String}, converted to uppercase.
2818      * @see     java.lang.String#toUpperCase(Locale)