java.lang.Character toTitleCase(char ch)
Description
The toTitleCase(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:
Character.toTitleCase(char ch)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java toTitleCase()Â method non statically.
Method Syntax
public static char toTitleCase(char ch)
Method Argument
| Data Type | Parameter | Description |
|---|---|---|
| char | ch | the character to be converted. |
Method Returns
The toTitleCase(char ch) method of Character class returns the titlecase equivalent of the character, if any; otherwise, the character itself.
Compatibility
Requires Java 1.0.2 and up
Java Character toTitleCase(char ch) Example
Below is a simple java example on the usage of toTitleCase(char ch) method of Character class.
package com.javatutorialhq.java.examples;
import java.util.Scanner;
/*
* This example source code demonstrates the use of
* toTitleCase(char ch) method of Character class.
*/
public class CharacterToTitleCaseCharExample {
public static void main(String[] args) {
// Ask for user input
System.out.print("Enter an input:");
// use scanner to get the user input
Scanner s = new Scanner(System.in);
// get a single character
char[] value = s.nextLine().toCharArray();
// close the scanner object
s.close();
// convert user input to title case
for(char ch:value){
char chTitleCase = Character.toTitleCase(ch);
// print the result
System.out.println("character "+ch +" Title case is " +chTitleCase);
}
}
}
Sample Output
Below is the sample output when you run the above example.
