Java - Character isTitleCase() Method



The Java Character isTitleCase() method determines if the specified character is a titlecase character.

A character is said to be a titlecase character if its general category type is TITLECASE_LETTER provided by Character.getType(ch) method.

For example, let us consider a character (Lj) that looks like a pair of basic Latin letters. The uppercase letter form of this character looks like "LJ" and the corresponding lowercase letter form looks like "lj". A third form, which looks like "Lj", is the appropriate form to use when we provide a word in lowercase with its initials as capitals. This is the Titlecase form.

This method comes in two polymorphic forms: one with a char value as its parameter and the other with an integer code point value as its parameter.

Syntax

Following is the syntax for Java Character isTitleCase() method

public static boolean isTitleCase(char ch)
(or)
public static boolean isTitleCase(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the argument is in titlecase, otherwise false.

Example

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

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

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'T';
      ch2 = '\u01f2'; 

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if ch1, ch2 are titlecase characters
       *  and assign results to b1, b2
       */
      b1 = Character.isTitleCase(ch1);
      b2 = Character.isTitleCase(ch2);
      String str1 = ch1 + " is a titlecase character is " + b1;
      String str2 = "ch2 is a titlecase character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

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

T is a titlecase character is false
ch2 is a titlecase character is true

Example

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

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

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x01c8;
      cp2 = 0x1f09;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if cp1, cp2 represent titlecase characters
       *  and assign results to b1, b2
       */
      b1 = Character.isTitleCase(cp1);
      b2 = Character.isTitleCase(cp2);
      String str1 = "cp1 represents a titlecase character is " + b1;
      String str2 = "cp2 represents a titlecase character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

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

cp1 represents a titlecase character is true
cp2 represents a titlecase character is false

Example

Since the method returns boolean values, we can also use this method as a condition to conditional statements in Java.

import java.lang.*;
public class Demo{   
   public static void main(String []args){
      char c = '\u1f8c';
      if(Character.isTitleCase(c))
         System.out.println("The character is a Titlecase Letter");
      else
         System.out.println("The character is not a Titlecase Letter");
   }
}

Output

Let us compile and run the program given to obtain the output as follows −

The character is a Titlecase Letter

Example

We can also create a user-defined function to make the program more efficient. In the following example, we create a method called “Titlecase” that returns whether the given character is a Titlecase letter or not. Then we pass the return value to the conditional statements to display the output.

import java.lang.*;
public class Demo{
   static boolean Titlecase(char ch) {
      return Character.isTitleCase(ch);
   }    
   public static void main(String []args){
      char c = 'a';
      if(Titlecase(c))
         System.out.println("The character is a Titlecase Letter");
      else
         System.out.println("The character is not a Titlecase Letter");
   }
}

Output

After compiling and running the program, the output is printed as shown below −

The character is not a Titlecase Letter
java_lang_character.htm
Advertisements