Java Locale getExtension() Method



Description

The Java Locale getExtension(char key) method returns the extension (or private use) value associated with the specified key, or null if there is no extension associated with the key. To be well-formed, the key must be one of [0-9A-Za-z]. Keys are case-insensitive, so for example 'z' and 'Z' represent the same extension.

Declaration

Following is the declaration for java.util.Locale.getExtension(char key) method

public String getExtension(char key)

Parameters

key − the extension key

Return Value

This method returns the extension, or null if this locale defines no extension for the specified key.

Exception

IllegalArgumentException − if key is not well-formed.

Getting Extension from a Locale Example

The following example shows the usage of Java Locale getExtension() method. We're creating a locale using Builder with a given extension with a key. Then using getExtension() method, extension is printed for an existing key and then it is printed for non-existent key.

package com.tutorialspoint;

import java.util.Locale;
import java.util.Locale.Builder;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = new Builder().setExtension('a', "sample-ex-tension").build();

      // print the extension for 'a'
      System.out.println("Extension:" + locale.getExtension('a'));

      // print the extension for 'b' as null
      System.out.println("Extension:" + locale.getExtension('b'));

   }
}

Output

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

Extension:sample-ex-tension
Extension:null
java_util_locale.htm
Advertisements