Java String Upper Case toUpperCase(String s)

Here you can find the source of toUpperCase(String s)

Description

Do an ASCII only upper case conversion.

License

Academic Free License

Declaration

public static String toUpperCase(String s) 

Method Source Code

//package com.java2s;
// Licensed under the Academic Free License version 3.0

public class Main {
    /**// w  w  w. jav a2s .  co m
     * Do an ASCII only upper case conversion.  Case conversion
     * with Locale can result in unexpected side effects.
     */
    public static char toUpperCase(char c) {
        if ('a' <= c && c <= 'z')
            return (char) (c & ~0x20);
        else
            return c;
    }

    /**
     * Do an ASCII only upper case conversion.  Case conversion
     * with Locale can result in unexpected side effects.
     */
    public static String toUpperCase(String s) {
        // first scan to see if string isn't already ok
        int len = s.length();
        int first = -1;
        for (int i = 0; i < len; ++i) {
            char a = s.charAt(i);
            char b = toUpperCase(a);
            if (a != b) {
                first = i;
                break;
            }
        }
        if (first == -1)
            return s;

        // allocate new char buf and copy up to first change
        char[] buf = new char[len];
        s.getChars(0, first, buf, 0);

        // change remainder of string
        for (int i = first; i < len; ++i)
            buf[i] = toUpperCase(s.charAt(i));

        return new String(buf);
    }
}

Related

  1. toUpperCase(String from)
  2. toUpperCase(String in)
  3. toUpperCase(String input)
  4. toUpperCase(String pString)
  5. toUpperCase(String s)
  6. toUpperCase(String s)
  7. toUpperCase(String s)
  8. toUpperCase(String s)
  9. toUpperCase(String s)