Escape percent sign in String’s format method in java

Escape percent sign in java

In this post, we will see how to escape Percent sign in String’s format() method in java.

Escape Percent Sign in String’s Format Method in Java

String’s format() method uses percent sign(%) as prefix of format specifier.
For example:
To use number in String’s format() method, we use %d, but what if you actually want to use percent sign in the String.

If you want to escape percent sign in String’s format method, you can use % twice (%%).

Let’s see with the help of example:

Output:

10 out of 100 is 10%

As you can see, we have used %% to escape percent symbol in 10%.

Escape Percent Sign in printf() Method in Java

You can apply same logic in printf method to print percent sign using System.out.printf() method.

Output:

10 out of 100 is 10%

That’s all about How to escape percent sign in String’s format method in java.

Was this post helpful?

Related Posts

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 25 May

    Get Variable Name as String in JavaScript

    Table of ContentsUsing Object.keys()Using toString() methodUsing hash tableWas this post helpful? In this post, we will see how to get variable name as String in JavaScript. There are multiple ways to get Get variable name as String in JavaScript. Let’s go through them. Using Object.keys() To get variable name as String in JavaScript, use Object.keys() […]

  • 25 May

    Format Date to yyyy-MM-dd in java

    Table of ContentsUsing DateTimeFormatter with LocalDate (Java 8)Using SimpleDateFormatUsing Calendar classWas this post helpful? In this post, we will see how to Format Date to yyyy-MM-dd in java. There are multiple ways to format Date to yyyy-MM-dd in java. Let’s go through them. Using DateTimeFormatter with LocalDate (Java 8) To Format Date to yyyy-MM-dd in […]

  • 25 May

    Remove Comma from String in JavaScript

    Table of ContentsUsing replace()Using replaceAll()Using split() and join() methodWas this post helpful? In this post, we will see how to remove comma from String in Javascript. There are multiple ways to do it. Let’s go through them. Using replace() To remove comma from String in Javascript: Use replace() method with regular expression /\,/g as first […]

  • 25 May

    Escape new line in JavaScript

    Table of ContentsEscape new line in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape new line in JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape new line in JavaScript To escape new line in JavaScript: Use replace() method with regular expression […]

  • 23 May

    Escape Ampersand in URL in JavaScript

    Table of ContentsGeneric solution to escape special character in JavaScriptWas this post helpful? In this post, we will see how to escape ampersand in JavaScript. To escape Ampersand in JavaScript, use encodeURIComponent() in JavaScript. [crayon-62b849c4cd241836182785/] Output [crayon-62b849c4cd244938548422/] As you can see that when we called encodeURIComponent, it encoded & to %26. You can use % […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.