{"id":89771,"date":"2024-07-22T18:00:25","date_gmt":"2024-07-22T12:30:25","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89771"},"modified":"2024-07-22T15:00:52","modified_gmt":"2024-07-22T09:30:52","slug":"stringbuilder-class-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/","title":{"rendered":"StringBuilder Class in Java with Examples"},"content":{"rendered":"<p>Mutable (modifiable) Strings can be created using the Java StringBuilder class. With the exception of being non-synchronized, the Java StringBuilder class is identical to the StringBuffer class. It has been accessible since JDK 1.5.<\/p>\n<h2>Explanation:<\/h2>\n<ul>\n<li>A character string that can be modified is represented by the StringBuilder Java class. In Java, the StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.<\/li>\n<li>Due to the fact that both classes build changing character sequences as an alternative to the String Class, their functions are very similar. The StringBuilder class, however, differs from the StringBuffer class in terms of synchronization.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public final class StringBuilder\r\n    extends Object\r\n    implements Serializable, CharSequence<\/pre>\n<h3>Class Hierarchy:<\/h3>\n<p class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">java.lang.Object<br \/>\n\u21b3 java.lang<br \/>\n\u21b3 Class StringBuilder<\/p>\n<h3>Constructors:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">StringBuilder()<\/span><\/td>\n<td>It creates a String Builder with no characters, and its capacity is 16.<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">StringBuilder(String str)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It creates a string builder and with no characters and its capacity specified capacity argument.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">StringBuilder(int length)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It creates an empty String Builder and the capacity is specified in length.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Methods in Java StringBuilder<\/h3>\n<p><strong>StringBuilder append(X x):<\/strong> This method appends the string representation of the X type argument to the sequence.<\/p>\n<ul>\n<li><strong>StringBuilder appendCodePoint(int codePoint):<\/strong> This method appends the string representation of the codePoint argument to this sequence.<\/li>\n<li><strong>int capacity():<\/strong> This approach gives the available capacity.<\/li>\n<li><strong>char charAt(int index):<\/strong> The char value in this sequence at the given index is returned by this procedure.<\/li>\n<li><strong>IntStream chars():<\/strong> The java.lang package contains the codePoints() function.String type. A stream of code point values from the supplied sequence is returned. Code points are specific character-specific Unicode code points.<\/li>\n<li>The character (Unicode code point) before the given index is returned by the method int codePointBefore(int index).<\/li>\n<li>The method IntStream codePoints() extracts a stream of code point values from this sequence.<\/li>\n<li><strong>StringBuilder delete(int start, int end):<\/strong> The java.lang.StringBuilder class contains the delete(int start, int end) function. The StringBuilder sequence&#8217;s substring in question is deleted. The start and end indices are used to specify the substring.<\/li>\n<li><strong>StringBuilder deleteCharAt(int index):<\/strong> The java.lang.StringBuilder class contains the deleteCharAt(int index) function. It eliminates the character from the StringBuilder sequence at the provided index.<\/li>\n<\/ul>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Methods-in-Java-StringBuilder.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89878\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Methods-in-Java-StringBuilder.webp\" alt=\"Methods in Java StringBuilder\" width=\"400\" height=\"210\" \/><\/a><\/p>\n<p><strong>Example :<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class StringBuilderExample{  \r\npublic static void main(String args[]){  \r\nStringBuilder sb=new StringBuilder(\"Hello \");  \r\nsb.append(\"World\");\/\/now original string is changed  \r\nSystem.out.println(sb);\/\/prints Hello Java  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello World<\/p>\n<h3>2. StringBuilder insert() method<\/h3>\n<p>By using the StringBuilder insert() method, the specified string is placed beside this string in the specified location.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class StringBuilder{  \r\npublic static void main(String args[]){  \r\nStringBuilder sb=new StringBuilder(\"Hello \");  \r\nsb.insert(1,\"World\");\/\/now original string is changed  \r\nSystem.out.println(sb);\r\n}  \r\n}<\/pre>\n<p><strong>Output :<\/strong><\/p>\n<p>HWorldello<\/p>\n<h3>3. StringBuilder replace() method<\/h3>\n<p>The replace() method of the StringBuilder replaces a string with the specified beginIndex and endIndex.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class StringBuilder{  \r\npublic static void main(String args[]){  \r\nStringBuilder sb=new StringBuilder(\"Hello\");  \r\nsb.replace(1,3,\"World\");  \r\nSystem.out.println(sb);\/\/prints HJavalo  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHWorldlo<\/p>\n<h3>4.StringBuilder delete() method<\/h3>\n<p>The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class StringBuilder{  \r\npublic static void main(String args[]){  \r\nStringBuilder a=new StringBuilder(\"Content\");  \r\nsb.delete(1,3);  \r\nSystem.out.println(a);  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nCnent<\/p>\n<h3>5.StringBuilder reverse() method<\/h3>\n<p>The current string is reversed by the StringBuilder class&#8217;s reverse() function.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class StringBuilder{  \r\npublic static void main(String args[]){  \r\nStringBuilder sb=new StringBuilder(\"Writing\");  \r\nsb.reverse();  \r\nSystem.out.println(sb);  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\ngnitirW<\/p>\n<h3>Methods of String ClassBuilder:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">public StringBuilder append(String s)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It&#8217;s used to add this string to the one that&#8217;s already there. The append() function has numerous overloads, including append(char), append(boolean), append(int), append(float), append(double), etc..<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public StringBuilder insert(int offset, String s)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The specified string is inserted at the appropriate location using this string. There are other insert() methods, such insert(int, char), insert(int, boolean), and others.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public StringBuilder replace(int startIndex, int endIndex, String str)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This method replaces the string from the startIndex and endIndex values.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public StringBuilder delete(int startIndex, int endIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to remove the string from the specified startIndex and endIndex.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public StringBuilder reverse()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The string is made backwards using it..<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int capacity()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to present the capacity at the moment.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public void ensureCapacity(int minimumCapacity)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to ensure that the capacity is at least equal to the minimum value given.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public char charAt(int index)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The character at the requested place is returned using it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int length()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This method returns the string&#8217;s length or the total number of characters.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public String substring(int beginIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returning the substring from the provided begin is its purpose. Index<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Functions:<\/h4>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/StringBuilder-Class-function-.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89879\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/StringBuilder-Class-function-.webp\" alt=\"StringBuilder Class function\" width=\"400\" height=\"210\" \/><\/a><\/p>\n<h3>Uses of StringBuilder Class:<\/h3>\n<p>When you wish to alter a string without generating a new object, you can use the StringBuilder class. The StringBuilder class, for instance, can improve speed when string concatenation is done repeatedly in a loop.<\/p>\n<h3>Advantages Of StringBuilder Class:<\/h3>\n<ul>\n<li>StringBuilder is mutable, in contrast to the String class, which is immutable (unchangeable)<\/li>\n<li><strong>Efficiency &#8211; <\/strong>It is mainly optimized for concatenating and manipulating the strings.<\/li>\n<li><strong>Reduce Memory &#8211;<\/strong> StringBuilder is mutable, so it reduces the memory.<\/li>\n<\/ul>\n<h3>Disadvantages of StringBuilder Class:<\/h3>\n<ul>\n<li><strong>Limited function &#8211;<\/strong>It only provides a few methods like appending, indexing and deleting<\/li>\n<li><strong>No built-in methods &#8211;<\/strong> It does not provide methods for operations like searching, splitting and replacing.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have extensively learned the StringBuilder Class. The function provides more detailed examples and explains them. It is mainly used with various functions in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mutable (modifiable) Strings can be created using the Java StringBuilder class. With the exception of being non-synchronized, the Java StringBuilder class is identical to the StringBuffer class. It has been accessible since JDK 1.5.&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447395,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,300,5659,327,5538,250,5590,302],"class_list":["post-89771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-stringbuilder-class","tag-java-stringbuilder-class-with-examples","tag-java-tutorials","tag-java-tutorials-for-beginners","tag-learn-java","tag-stringbuilder-class","tag-stringbuilder-class-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>StringBuilder Class in Java with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.\" \/>\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\/stringbuilder-class-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StringBuilder Class in Java with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/\" \/>\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-07-22T12:30:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\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":"StringBuilder Class in Java with Examples - TechVidvan","description":"Java StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.","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\/stringbuilder-class-in-java\/","og_locale":"en_US","og_type":"article","og_title":"StringBuilder Class in Java with Examples - TechVidvan","og_description":"Java StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.","og_url":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-07-22T12:30:25+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp","type":"image\/webp"}],"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\/stringbuilder-class-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"StringBuilder Class in Java with Examples","datePublished":"2024-07-22T12:30:25+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/"},"wordCount":805,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp","keywords":["java","java stringBuilder class","java stringbuilder class with examples","java tutorials","java tutorials for beginners","Learn Java","stringbuilder class","stringBuilder class in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/","name":"StringBuilder Class in Java with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp","datePublished":"2024-07-22T12:30:25+00:00","description":"Java StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-string-builder-class.webp","width":1200,"height":628,"caption":"java stringbuilder class"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/stringbuilder-class-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"StringBuilder Class in Java 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\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89771","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\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89771"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89771\/revisions"}],"predecessor-version":[{"id":447540,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89771\/revisions\/447540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447395"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}