getExtension(): This method is available in java.util.Locale class of Java.
Syntax:
String java.util.Locale.getExtension(char key)
This method takes one argument of type char as its parameter. This method returns the extension (or private use) value associated with the specified key, or null if there is no extension associated with the key.
Note: 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.
Parameters: One parameter is required for this method.
key: the extension key.
Returns: The extension, or null if this locale defines no extension for the specified key.
Throws: IllegalArgumentException - if the key is not well-formed.
For Example:
Locale locale = new Locale("en", "USA", "US")
char key = 'Z'
locale.getExtension(key) = > It returns null.
Approach 1: When the key is well formed
Java
import java.util.Locale;public class LocalegetExtension {public static void main(String[] args) {Locale locale = new Locale("en", "USA", "US");char key = 'Z';System.out.println(locale.getExtension(key));}}
Output:
null
Approach 2: When the key is not well-formed.
Java
import java.util.Locale;public class LocalegetExtension {public static void main(String[] args) {Locale locale = new Locale("en", "USA", "US");char key = '@';System.out.println(locale.getExtension(key));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException:Ill-formed extension key: @ at java.base/java.util.Locale.getExtension(Locale.java:1299)
No comments:
Post a Comment