Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet65/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   61   62   63   64   65   66   67   68   ...   245
Bog'liq
Java 17 Recipes

 How It Works
When you require dynamic string content, the format() utility can come in handy. The 
built-in format() method allows one to position a placeholder within a string, such 
that the placeholder is replaced with dynamic content at runtime. The format method 
accepts a string and a series of variables that displaces the placeholders within the string 
with dynamic content at runtime. The placeholders must be designated specifically for 
the type of content with which they are displaced. Each placeholder must begin with a 
% character to denote a placeholder within the string. The placeholder can also contain 
flags, width, and precision indicators to help format the dynamic value. The following 
format should build each placeholder.
%[flags][width][.precision]conversion_indicator
The second solution demonstrates how to utilize the System.out.printf() 
method, which accepts the same arguments as the System.format() method. The main 
difference is that the System.out.printf() method is handy for printing formatted 
content. If your application requires storing a formatted value, you are more likely to use 
the String.format() method.
Chapter 3 StringS


106
3-17. Summary
This chapter covered the basics of working with strings. Although a string may look 
like a simple string of characters, it is an object that contains many methods that can 
be useful for obtaining the required results. Although strings are immutable objects, 
many methods within the String class contain a copy of the string, modified to suit 
the request. The chapter also covered a handful of these methods, demonstrating 
features such as concatenation, obtaining portions of strings, trimming whitespace, and 
replacing portions of a string.
Chapter 3 StringS


107
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_4

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   61   62   63   64   65   66   67   68   ...   245




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