Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

How to format a Java String with printf example

learningcode_x1mckf by learningcode_x1mckf
January 1, 2023
in Java
0
How to format a Java table with printf example
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Learn how to printf a Java String

To make use of the Java String printf technique to format output textual content, observe these guidelines:

  1. Use %s because the printf String specifier within the textual content string being formatted.
  2. Use %S because the printf String specifier to output upper-case textual content.
  3. Precede the letter s with a quantity to specify area width.
  4. Put a adverse signal after the % to left-justify the textual content.
  5. Add %n any time you want to embrace a line break.

Java String printf instance

Right here is an easy instance of easy methods to format a Java String with printf:

public class JavaPrintfStringExample 
  /* Java String printf instance code. */
  public static void important(String[] args) 
    String identify = "Cameron";
    String web site = "Tss";
    System.out.printf("I just like the articles %s writes on %S. %n", identify, web site);
    /* Output: I just like the articles Cameron writes on TSS. */
  

The instance above outputs the next textual content:

I just like the articles Cameron writes on TSS.

This can be a easy instance of easy methods to format a Java String with printf, however it additionally demonstrates two fascinating options of the syntax:

  1. Using %s (lowercase s) preserves the casing of the identify ‘Cameron’
  2. Using %S (uppercase S) makes the casing of Tss uppercase.

Using %n forces a line break, which isn’t seen within the output of this instance, however it is a vital component to incorporate when formatting paragraphs of textual content.

Advantages of the Java String printf technique

The Java String printf technique could be complicated at first, however it enormously simplifies the way you format advanced Strings. That is very true when creating sophisticated database, NoSQL or Hibernate queries.

Examine the Java String printf instance above with how we must format a textual content String with out printf. The next code snippet demonstrates the distinction between a String formatted with printf, and one created by means of String addition:


String identify = "Cameron";
String web site = "Tss";

/* The next line makes use of the Java String printf syntax. */
System.out.println( "I just like the articles " + identify + "  writes on " + web site + "." );

/* The next line makes use of the Java String printf syntax. */ 
System.out.printf( "I just like the articles %s writes on %S.%n", identify, web site );

Even with this straightforward instance, you may see how the Java printf assertion codecs Strings which might be a lot simpler to digest. As Strings turn into more and more sophisticated, Java’s printf technique makes these Strings considerably simpler to construct.

Java String printf syntax

The format for the Java printf perform is as follows:

% [flag] [width] specifier-character (s or S)

The flag most sometimes used with a Java String printf assertion is the adverse signal, which left-aligns the textual content.

The width specifier is an integer that specifies what number of areas must be devoted to displaying the String. If the String is shorter than the quantity specified, whitespace is displayed adjoining to the String.

Java String printf format examples
printf flag Function
 %s Show the textual content with higher and lowercasing preserved
 %S Show the textual content in all uppercase letters
 %-s Alight the textual content to the left, with String casing preserved
%20s Allocate 20 areas to show the right-aligned textual content
%-10S Allocate 20 areas to show the left-aligned textual content and use uppercase lettering

Java String printf desk

A standard approach to reveal superior Java printf String utilization is to format a table of information.

The superior Java String printf instance under will generate the next desk of information:

The information desk generated from the Java printf technique instance.

Java String printf desk instance

Right here is the total code for the superior Java String printf desk instance:

System.out.printf("--------------------------------%n");
System.out.printf(" Java's Primitive Varieties         %n");
System.out.printf(" (String printf instance)        %n");

System.out.printf("--------------------------------%n");
System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");
System.out.printf("--------------------------------%n");

System.out.printf("| %-10s | %-8s | %4s |%n", "Floating", "double",  "0064");
System.out.printf("| %-10s | %-8s | %4s |%n", "Floating", "float",   "0032");
System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "lengthy",    "0064");
System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "int",     "0032");
System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "char",    "0016");
System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "quick",   "0016");
System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "byte",    "0008");
System.out.printf("| %-10s | %-8s | %4s |%n", "Boolean",  "boolean", "0001");

System.out.printf("--------------------------------%n");

Output with Java String printf

Java Printf String example

Learn how to format a String with Java printf technique calls.

When the code runs, the output is an easy desk that shows details about the 8 Java primitive types.

It additionally offers a fantastic instance of easy methods to use the Java printf technique to format String-based output.

 

 



Source link

You might also like

Java Developer Survey Reveals Increased Need for Java … – PR Newswire

What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy

Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Java Developer Survey Reveals Increased Need for Java … – PR Newswire

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Developer Survey Reveals Increased Need for Java ...  PR Newswire Source link

Read more

What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

What You Should Definitely Pay Attention to When Hiring Java Developers  Trendy Diplomacy Source link

Read more

Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Web Frameworks Software Market Research Report 2023 ...  Los Alamos Monitor Source link

Read more

Minecraft Java Edition: 10 Best World Editors – TheGamer

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Minecraft Java Edition: 10 Best World Editors  TheGamer Source link

Read more

Oracle Releases Java 20 – PR Newswire

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle Releases Java 20  PR Newswire Source link

Read more
Next Post
Is JavaScript Token (JS) Worth the Risk Sunday?

Is JavaScript Token (JS) Worth the Risk Sunday?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

“I’m a C++ developer, it took me a week to learn Rust”

“I’m a C++ developer, it took me a week to learn Rust”

September 15, 2022
Senior Java Developer at Datonomy Solutions – Gauteng Johannesburg North

Senior Java Developer at Datonomy Solutions

November 23, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Aerospike targets Java Spring devs with support for the popular framework – The Register

March 22, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?