{"id":89287,"date":"2024-02-22T18:00:54","date_gmt":"2024-02-22T12:30:54","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89287"},"modified":"2024-02-22T18:00:54","modified_gmt":"2024-02-22T12:30:54","slug":"java-string-format-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/","title":{"rendered":"Java String format() Method with Examples"},"content":{"rendered":"<p>Java is simple to understand and implement code in a variety of platforms. We are going to learn about String format(). String format() is used to create formatted String in format pattern. It is widely used for applications, formatting numbers and dates.<\/p>\n<h2>Explanation<\/h2>\n<p>The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.<\/p>\n<p>This method allows us to concatenate the texts while simultaneously formatting the resultant concatenated string.<\/p>\n<p>Java&#8217;s format() method is similar to the sprintf() function in C and the printf() method in Java.<\/p>\n<h3>How to use Java string format():<\/h3>\n<p>The format() method accepts several parameters, including locale, format, and object, which carry the values of the locale, string format, and object arguments.<\/p>\n<h4>Syntax:<\/h4>\n<p><strong>The following list of string format() functions includes two different types:<\/strong><\/p>\n<ul>\n<li>public static String format(Locale loc, String form, Object&#8230; args)<\/li>\n<li>public static String format(String form, Object).<\/li>\n<\/ul>\n<p><strong>Syntax explanation :<\/strong><\/p>\n<p>public static String format(Locale loc, String form, Object&#8230; args)<\/p>\n<ul>\n<li>format() is a static method. We call the format() method using the class name String.<\/li>\n<li>str is a string that is to be formatted<\/li>\n<li>&#8230; in the above code signifies you can pass more than one object to format().<\/li>\n<\/ul>\n<p>public static String format(String form, Object).<\/p>\n<ul>\n<li>format is the String object that represents the format for the output String.<\/li>\n<li>&#8230;args are zero or more number of arguments that the String object &#8220;format&#8221; could need.<\/li>\n<\/ul>\n<h4>Parameter:<\/h4>\n<ul>\n<li><strong>Locale: <\/strong>The format() method&#8217;s locale is specified by the locale argument.<\/li>\n<li><strong>format:<\/strong> The string&#8217;s format.<\/li>\n<li><strong>args:<\/strong> The format string&#8217;s parameters. It could be 0 or more.<\/li>\n<\/ul>\n<h4>Return Value:<\/h4>\n<ul>\n<li>Return Formatted string<\/li>\n<\/ul>\n<h3>Implementation of Java string format():<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public static String format(String format, Object... args) {  \n       return new Formatter().format(format, args).toString();  \n   }<\/pre>\n<h3>Exception:<\/h3>\n<ul>\n<li>NullPointerException is the first exception.If the argument format String object is null, this exception occurs.<\/li>\n<li><strong>IllegalFormatException:<\/strong> This exception happens if the format argument String is invalid or there are insufficient arguments.<\/li>\n<\/ul>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/10\/Java-String-format-Diagram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89289 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/10\/Java-String-format-Diagram.webp\" alt=\"Java String format Diagram\" width=\"962\" height=\"386\" \/><\/a><\/p>\n<h4>Example 1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanFormat{  \npublic static void main(String args[]){  \nString name=\"Ram\";  \nString sf1=String.format(\"The name is %s\",name);  \nString sf2=String.format(\"value1 is %f\",32.33);  \nString sf3=String.format(\"value2 is %d\",32);  \n\nSystem.out.println(sf1);  \nSystem.out.println(sf2);  \nSystem.out.println(sf3);  \n}\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe name is Ram<br \/>\nValue1 is 32.33<br \/>\nValue2 is 32<\/p>\n<h4>Example 2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanFormat {  \n    public static void main(String[] args) {  \n        String str1 = String.format(\"%d\", 132);          \/\/ Integer value  \n        String str2 = String.format(\"%s\", \"Java\"); \/\/ String value  \n        String str3 = String.format(\"%f\", 10.32);       \/\/ Float value  \n        String str4 = String.format(\"%x\", 100);          \/\/ Hexadecimal value  \n        String str5 = String.format(\"%c\", 'A');          \/\/ Char value  \n        System.out.println(str1);  \n        System.out.println(str2);  \n        System.out.println(str3);  \n        System.out.println(str4);  \n        System.out.println(str5);  \n    }  \n }<\/pre>\n<p><strong>Output:<\/strong><br \/>\n132<br \/>\nJava<br \/>\n10.320000<br \/>\n64<br \/>\nA<\/p>\n<h4>Example 3:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanFormat{  \n    public static void main(String[] args) {          \n        String str1 = String.format(\"%d\", 101);  \n        String str2 = String.format(\"|%10d|\", 101);  \/\/ Specifying length of integer  \n        String str3 = String.format(\"|%-10d|\", 101); \/\/ Left-justifying within the specified width  \n        String str4 = String.format(\"|% d|\", 101);   \n        String str5 = String.format(\"|%010d|\", 101); \/\/ Filling with zeroes  \n        System.out.println(str1);  \n        System.out.println(str2);  \n        System.out.println(str3);  \n        System.out.println(str4);  \n        System.out.println(str5);  \n    }  \n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n101<br \/>\n| 101|<br \/>\n|101 |<br \/>\n| 101|<br \/>\n|0000000101|<\/p>\n<h3>Table:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">Specifier<\/span><\/td>\n<td><span style=\"font-weight: 400\">Description<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%s, %S<\/span><\/td>\n<td><span style=\"font-weight: 400\">string<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%c, %C<\/span><\/td>\n<td><span style=\"font-weight: 400\">Unicode character<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%d<\/span><\/td>\n<td><span style=\"font-weight: 400\">decimal integer<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%o.<\/span><\/td>\n<td><span style=\"font-weight: 400\">an octal integer<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%x, %X<\/span><\/td>\n<td><span style=\"font-weight: 400\">hexadecimal integer<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%f.<\/span><\/td>\n<td><span style=\"font-weight: 400\">for decimal numbers<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%e, %E\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">for scientific notation<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Format specifiers:<\/h3>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/02\/java-string-format.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-90477 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/02\/java-string-format.webp\" alt=\"java string format\" width=\"700\" height=\"349\" \/><\/a><\/p>\n<h4>Format(IFormatProvider, String, Object, Object, Object):<\/h4>\n<p>The string representation of three supplied objects replaces the format components in a string. A parameter specifies culture-specific formatting.<\/p>\n<h4>Format(String, Object, Object, Object)<\/h4>\n<p>The string representation of three supplied objects replaces the format components in a string.<\/p>\n<h4>Format(IFormatProvider, String, Object, Object)<\/h4>\n<p>The string representation of two supplied objects replaces the format components in a string. A parameter specifies culture-specific formatting.<\/p>\n<h4>Format(String, Object, Object)<\/h4>\n<p>The string representation of two supplied objects replaces the format components in a string.<\/p>\n<h4>Format(IFormatProvider, String, Object)<\/h4>\n<p>The string representation of the corresponding object replaces the format item or items in a specified string. A parameter specifies culture-specific formatting.<\/p>\n<h4>Format(IFormatProvider, String, Object[])<\/h4>\n<p>The format components in a string are replaced with string representations of corresponding objects in a given array. A parameter specifies culture-specific formatting.<\/p>\n<h4>Format(String, Object[])<\/h4>\n<p>Replaces the format item in a given string with the string representation of a given array&#8217;s associated object.<\/p>\n<h4>Format(String, Object)<\/h4>\n<p>The string representation of a specified object replaces one or more format components in a string.<\/p>\n<h3>Usage of string format() in Java:<\/h3>\n<ul>\n<li>Numbering format<\/li>\n<li>Datatype format<\/li>\n<li>Formatting in the locale<\/li>\n<li>Date Formatting<\/li>\n<li>Formatted control output<\/li>\n<li>Formatting control<\/li>\n<li>Managing Spacing<\/li>\n<li>Alignment management<\/li>\n<li>Keeping the number of integral digits under control<\/li>\n<li>Managing the number of digits following the decimal separator<\/li>\n<li>The use of literal braces in a result string<\/li>\n<\/ul>\n<h3>Advantages of String format():<\/h3>\n<ul>\n<li><strong>Formatting: <\/strong>It allows you to simply format strings in format specifiers.<\/li>\n<li>The code is simple to read and understand.<\/li>\n<li>Because it is a formatted string, it is simple to save and reuse.<\/li>\n<\/ul>\n<h3>Disadvantage of string format():<\/h3>\n<ul>\n<li>Because of the extensive formatting, it is more difficult to read and maintain the code.<\/li>\n<li>Using long formatted strings consumes more memory.<\/li>\n<li>It does not return all errors in a formatted string.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have thoroughly discussed the String format() function in Java. It is simple to understand and implement the format in code. It provides efficient string formatting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is simple to understand and implement code in a variety of platforms. We are going to learn about String format(). String format() is used to create formatted String in format pattern. It is&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":89291,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5367,5368,250,5369,5370],"class_list":["post-89287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-format-method-with-examples","tag-java-string-format","tag-learn-java","tag-string-format","tag-string-format-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String format() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String format() Method with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-22T12:30:54+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String format() Method with Examples - TechVidvan","description":"The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String format() Method with Examples - TechVidvan","og_description":"The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-02-22T12:30:54+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java String format() Method with Examples","datePublished":"2024-02-22T12:30:54+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/"},"wordCount":695,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#primaryimage"},"thumbnailUrl":"","keywords":["java","java string format method with examples","java string format()","Learn Java","string format()","string format() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/","name":"Java String format() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#primaryimage"},"thumbnailUrl":"","datePublished":"2024-02-22T12:30:54+00:00","description":"The Java String format() method uses the specified locale, format string, and parameters to return a formatted string.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-format-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String format() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89287"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}