Java - Character toTitleCase() Method



The Java Character toTitleCase() method converts the character argument to titlecase using case mapping information from the UnicodeData file.

According to UnicodeData file, case is defined as the inherent property of a character. Case mappings in this file are informative and default mappings. For example, if a character is CAPITAL by default, then its corresponding lowercase is informative.

If a character has no explicit titlecase mapping; and is not a titlecase character in itself; according to the UnicodeData file, the uppercase mapping is returned as an equivalent titlecase mapping.

If a character is already present in the Titlecase, the method will return the original character as itself.

Syntax

Following is the syntax for Java Character toTitleCase() method

public static char toTitleCase(char ch)
(or)
public static int toTitleCase(int codePoint)

Parameters

  • ch − the character to be converted

  • codePoint − the Unicode code point to be converted

Return Value

This method returns the titlecase equivalent of the character, if any; otherwise, the character itself.

Example

The following example shows the usage of Java Character toTitleCase(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 4 char primitives
      char ch1, ch2, ch3, ch4;

      // assign values to ch1, ch2
      ch1 = 'p';
      ch2 = 'x';

      // assign titlecase of ch1, ch2 to ch3, ch4
      ch3 = Character.toTitleCase(ch1);
      ch4 = Character.toTitleCase(ch2);
      String str1 = "Titlecase of " + ch1 + " is " + ch3;
      String str2 = "Titlecase of " + ch2 + " is " + ch4;

      // print ch3, ch4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Titlecase of p is P
Titlecase of x is X

Example

The following example shows the usage of Java Character toTitleCase(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 4 int primitives
      int cp1, cp2, cp3, cp4;

      // assign values to cp1, cp2
      cp1 = 0x0067; // represents g
      cp2 = 0x005e; // represents ^

      // assign titlecase of cp1, cp2 to cp3, cp4
      cp3 = Character.toTitleCase(cp1);
      cp4 = Character.toTitleCase(cp2);
      String str1 = "Titlecase equivalent of " + cp1 + " is " + cp3;
      String str2 = "Titlecase equivalent of " + cp2 + " is " + cp4;

      // print cp3, cp4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Titlecase equivalent of 103 is 71
Titlecase equivalent of 94 is 94

Example

The following example shows the usage of the method to convert the characters into titlecase characters when the arguments given to the method are digit characters.

import java.lang.*;
public class CharacterDemo {
   public static void main(String args[]) {
      char c1 = '5';
      char c2 = Character.toTitleCase(c1);
      System.out.println("The Titlecase value of " + c1 + " is " + c2);
   }
}

Output

Let us compile and run the program above and the output will be achieved as follows −

The Titlecase value of 5 is 5

Example

In this example program below, the method takes symbols as character arguments and tries to convert them to Titlecase values

import java.lang.*;
public class CharacterDemo {
   public static void main(String args[]) {
      char ch = '$';
      char result = Character.toTitleCase(ch);
      System.out.println("The Titlecase value of " + ch + " is " + result);
   }
}

Output

Let us compile and run the given program; the output is then produced as follows −

The Titlecase value of $ is $
java_lang_character.htm
Advertisements