
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
String Formatting in Java using %
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.
- Related Articles
- String Formatting in Python using %?
- String Formatting in C# using %
- Formatting day of week using SimpleDateFormat in Java
- String Formatting Operator in Python
- How to print a complete tuple in Python using string formatting?
- String Formatting with ToString in C#
- Locale-specific formatting in Java
- How does string formatting work in PowerShell?
- String Formatting in C# to add padding
- Date Formatting Using printf
- Date Formatting Using SimpleDateFormat
- Formatting Text Using CSS
- Change date formatting symbols in Java
- Formatting Minute in m format in Java
- Formatting day in d format in Java

Advertisements