Convert a first character of a word or string of words into a different case, (upper to lower and lower to upper case) - Java java.lang

Java examples for java.lang:String Case

Introduction

The following code shows how to Convert a first character of a word or string of words into a different case, (upper to lower and lower to upper case).

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String term = "java2s.com";
        System.out.println(changeFirstCharCase(term));
    }//w ww. jav a  2s .  co m

    /**
     * <p>Convert a first character of a word or string
     * of words into a different case, (upper to lower and
     * lower to upper case)
     * @param term
     * @return word or N-Grams with a different case, or null, or unchanged for non-letter
     */
    public static String changeFirstCharCase(final String term) {
        String convertedString = null;
        int firstHexChar = (int) (term.charAt(0));

        /*
         * If this is a upper case, convert to lower case.
         */
        if (firstHexChar > 0x40 && firstHexChar < 0x5B) {
            char[] charsSeq = term.toCharArray();
            charsSeq[0] = (char) (firstHexChar + 0x20);
            convertedString = String.valueOf(charsSeq);
        }

        /*
         * If this is a lower case, convert to upper case.
         */
        else if (firstHexChar > 0x60 && firstHexChar < 0x7B) {
            char[] charsSeq = term.toCharArray();
            int hexChar = (int) charsSeq[0] - 0x20;
            charsSeq[0] = (char) hexChar;
            convertedString = String.valueOf(charsSeq);
        } else {
            convertedString = term;
        }

        return convertedString;
    }
}

Related Tutorials