Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet207/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   203   204   205   206   207   208   209   210   ...   245
Bog'liq
Java 17 Recipes

 How It Works
In Java 8, methods were added to the java.util.Locale class to filter Locale instances 
or language tags based on a supplied priority list in List 
format. The following list contains a short summary of these filtering methods.
• filter(List, Collection)
filter(List, Collection,
Locale.FilteringMode)
(Returns matching list of Locale instances)
• filterTags(List, Collection)
filterTags(List, Collection,
Locale.FilteringMode)
(Returns matching list of language tags)
To work with each method, a sorted priority order should be sent as the first 
parameter. This priority order is a list of Locale.LanguageRange objects should be sorted 
in descending order, based on priority or weight. The second argument in the filter() 
methods is a collection of locales. This collection contains the locales that will be 
filtered. The optional third argument contains a Locale.FilteringMode.
11-4. Searching Unicode with Regular Expressions
 Problem
You want to find or match Unicode characters in a string. You want to do that using 
regular expression syntax.
 Solution 1
The easiest way to find or match characters is to use the String class. String instances 
store Unicode character sequences and provide relatively simple operations for finding
replacing, and tokenizing characters using regular expressions.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


409
To determine whether a string matches a regular expression, use the matches() 
method. The matches() method returns true if the entire string matches the regular 
expression exactly.
The following code from the org.java17recipes.chapter11.recipe11_04.Recipe11_4 
class uses two different expressions with two strings. The regular expression matches 
simply confirm that the strings match a particular pattern as defined in the enRegEx and 
jaRegEx variables.
private String enText = "The fat cat sat on the mat with a brown rat.";
private String jaText = "Fight 文字化け!";
String enRegEx = "^The \\w+ cat.*";
String jaRegEx = ".*文字.*";
String jaRegExEscaped = ".*\u6587\u5B57.*";
public static void main(String[] args) {
Recipe11_4 app = new Recipe11_4();
app.run();
}
public void run() {
demoStringMatch();
demoStringReplace();
demoStringSplit();
demoSimple();
demoComplex();
}
public void demoStringMatch() {
boolean found = false;
found = enText.matches(enRegEx);
if (found) {
System.out.printf("Matches %s.\n", enRegEx);
}
found = jaText.matches(jaRegEx);
if (found) {
System.out.printf("Matches %s.\n", jaRegEx);
}
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


410
found = jaText.matches(jaRegExEscaped);
if (found) {
System.out.printf("Matches %s.\n", jaRegExEscaped);
}
}
This code prints the following.
Matches ^The \w+ cat.*.
Matches .*文字.*.
Matches .*文字.*.
Use the replaceFirst() method to create a new String instance in which the first 
occurrence of the regular expression in the target text is replaced with the replacement 
text. The code demonstrates how to use this method.
String replaced = jaText.replaceFirst("文字化け", "mojibake");
System.out.printf("Replaced: %s\n", replaced);
The replacement text is shown in the output.
Replaced: Fight mojibake!
The replaceAll() method replaces all occurrences of the expression with the 
replacement text.
Finally, the split() method creates a String[] that contains text separated by the 
matched expression. In other words, it returns text that is delimited by the expression. 
Optionally, you can provide a limit argument that constrains the number of times the 
delimiter is applied in the source text. The following code demonstrates the split() 
method splitting on space characters.
String[] matches = enText.split("\\s", 3);
for(String match: matches) {
System.out.printf("Split: %s\n",match);
}
The code’s output is as follows.
Split: The
Split: fat
Split: cat sat on the mat with a brown rat.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


411

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   203   204   205   206   207   208   209   210   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling