Java Locale stripExtensions() Method



Description

The Java Locale stripExtensions() method returns a copy of this Locale with no extensions. If this Locale has no extensions, this Locale is returned.

Declaration

Following is the declaration for java.util.Locale.stripExtensions() method

public Locale stripExtensions()

Parameters

NA

Return Value

This method returns a copy of this Locale with no extensions, or this if this has no extensions.

Exception

NA

Getting Copy of Locale without Extension Example

The following example shows the usage of Java Locale stripExtensions() 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. Now using stripExtensions() we're getting a copy of Locale without extensions and then check is made using getExtension() method to print the earlier existing value to be null.

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'));

      // create a extension free locale
      Locale locale1 = locale.stripExtensions();

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

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