
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String Formatting in Java Using Format Method
Followimg is the code to implement String formatting in Java using % −
Example
public class Demo { public static void main(String args[]){ String my_str = " sample."; String concat_Str = String.format("This is a" + "%s", my_str); String format_str_1 = String.format("The value is %.4f", 78.92367); System.out.println(concat_Str); System.out.println(format_str_1); } }
Output
This is a sample. The value is 78.9237
A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a floating point number is also formatted using the ‘%’ operator. Both these values are printed on the console.
Advertisements