{"id":89737,"date":"2024-09-23T18:00:23","date_gmt":"2024-09-23T12:30:23","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89737"},"modified":"2024-09-23T18:59:32","modified_gmt":"2024-09-23T13:29:32","slug":"java-string-join-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/","title":{"rendered":"Java String join() Method with Examples"},"content":{"rendered":"<p>The join() method requires two inputs.<\/p>\n<p><strong>Elements:<\/strong>\u00a0the items that need to be linked together delimiter &#8211; the delimiter Any class that implements CharSequence may be sent to join().<\/p>\n<p>If one is passed, the elements of an iterable will be combined. The iterable must implement CharSequence. Because these classes implement CharSequence, they are CharSequence.<\/p>\n<p>When the static String concatenates each string, a delimiter is placed between them. The join() method, which is part of the String class, creates a single string by joining the provided string and the delimiter.<\/p>\n<h2>Syntax:<\/h2>\n<p class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Both public static String join (CharSequence deli, Iterable? extends CharSequence&gt; ele) and public static String join (CharSequence deli, CharSequence&#8230; ele) are available.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<p>Each element must have a delimiter associated with it. The delimiter may be a string or character.<\/p>\n<p>Returns a joined string with a delimiter.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Techvidvan{\r\n    public static void main(String args[])\r\n    {\r\n        \/\/ delimiter is \"&lt;\" and elements are \"Four\", \"Five\", \"Six\", \"Seven\"\r\n        String gfg1 = String.join(\" &lt; \", \"Four\", \"Five\", \"Six\", \"Seven\");\r\n  \r\n        System.out.println(gfg1);\r\n    }\r\n}<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/java-String-join-example-1-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89894\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/java-String-join-example-1-1.webp\" alt=\"java String join() example\" width=\"400\" height=\"210\" \/><\/a><\/p>\n<p><strong>For instance, omitting the StringJoiner class and the String.join() function Approach:<\/strong><\/p>\n<p>We won&#8217;t be using Java 8 in this program; instead, we&#8217;ll attempt to complete it manually and see how much larger a code base or number of steps were required to accomplish our objective.<\/p>\n<p>First, we will unite the three strings &#8220;DSA,&#8221; &#8220;FAANG,&#8221; and &#8220;ALGO,&#8221; using the delimiter &#8220;gfg.&#8221; To do this, we must write the delimiter each time before a new string is added.<\/p>\n<p>We will next unite the components of an ArrayList containing the strings &#8220;DSA,&#8221; &#8220;FAANG,&#8221; and &#8220;ALGO&#8221; using the delimiter &#8220;gig. &#8221; To do this, we had to iterate over the ArrayList.<\/p>\n<p><strong>Notes:<\/strong><\/p>\n<p><strong>Elements:<\/strong> A char value with a delimiter must be appended, and a combined string with a delimiter is returned.<\/p>\n<p>If the element or delimiter is null, the exception throws a NullPointerException.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ Java program to demonstrate \r\n\/\/ working of join() method \r\n\r\n\r\nclass Gfg3 { \r\n  public static void main(String args[]) \r\n  { \r\n    \/\/ delimiter is \"-&gt;\" and elements are \"Wake up\", \r\n    \/\/ \"Eat\", \"Play\", \"Sleep\", \"Wake up\" \r\n\r\n\r\n    String gfg3 = String.join(\"-&gt; \", \"Wake up\", \"Eat\", \r\n          \"Play\", \"Sleep\", \"Wake up\"); \r\n\r\n\r\n    System.out.println(gfg3); \r\n  } \r\n} \r\npublic class StringJoinExample3   \r\n{  \r\n\/\/ main method  \r\npublic static void main(String argvs[])  \r\n{  \r\nString str = null;  \r\nstr = String.join(null, \"abc\", \"bcd\", \"apple\");  \r\nSystem.out.println(str);  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Exception in thread &#8220;main&#8221; java.lang.NullPointerException<br \/>\nat java.base\/java.util.Objects.requireNonNull(Objects.java:221)<br \/>\nat java.base\/java.lang.String.join(String.java:2393)<br \/>\nat StringJoinExample3.main(StringJoinExample3.java:7)<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Example\r\n{\r\n   public static void Main()\r\n   {\r\n      int maxPrime = 100;\r\n      List&lt;int&gt; primes = GetPrimes(maxPrime);\r\n      Console.WriteLine(\"Primes less than {0}:\", maxPrime);\r\n      Console.WriteLine(\"   {0}\", String.Join(\" \", primes));\r\n   }\r\n\r\n\r\n   private static List&lt;int&gt; GetPrimes(int maxPrime)\r\n   {\r\n      Array values = Array.CreateInstance(typeof(int), \r\n                              new int[] { maxPrime - 1}, new int[] { 2 });\r\n      \/\/ Use Sieve of Eratosthenes to determine prime numbers.\r\n      for (int ctr = values.GetLowerBound(0); ctr &lt;= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)\r\n      {\r\n                           \r\n         if ((int) values.GetValue(ctr) == 1) continue;\r\n         \r\n         for (int multiplier = ctr; multiplier &lt;=  maxPrime \/ 2; multiplier++)\r\n            if (ctr * multiplier &lt;= maxPrime)\r\n               values.SetValue(1, ctr * multiplier);\r\n      }      \r\n      \r\n      List&lt;int&gt; primes = new List&lt;int&gt;();\r\n      for (int ctr = values.GetLowerBound(0); ctr &lt;= values.GetUpperBound(0); ctr++)\r\n         if ((int) values.GetValue(ctr) == 0) \r\n            primes.Add(ctr);\r\n      return primes;\r\n   }   \r\n}\r\n\r\n\r\n\/\/ The example displays the following output:\r\n\/\/    Primes less than 100:\r\n\/\/       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97<\/pre>\n<p><strong>NOTES:<\/strong><\/p>\n<p>String is returned.<br \/>\nA string made up of value items separated by a separator string.<\/p>\n<p><strong>-or-<\/strong><\/p>\n<p>If values contain no elements, it is empty.<\/p>\n<h3>Exceptions<\/h3>\n<p>The values of ArgumentNullException are null.<\/p>\n<h3>OutOfMemoryException<\/h3>\n<p>The generated string&#8217;s length exceeds the maximum allowable length (Int32.MaxValue).<\/p>\n<h3>Conclusion<\/h3>\n<p>The java string join() method connects all strings with a delimiter specified by the user. Because this method is a static method of the Java String class, we don&#8217;t need to construct any objects to use it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The join() method requires two inputs. Elements:\u00a0the items that need to be linked together delimiter &#8211; the delimiter Any class that implements CharSequence may be sent to join(). If one is passed, the elements&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447400,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5596,288,289,327,250,5598,5599],"class_list":["post-89737","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-string-join","tag-java-string-join-method","tag-java-string-join-method-with-example","tag-java-tutorials","tag-learn-java","tag-string-join-method-in-java","tag-string-join-with-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String join() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"The java string join() method connects all strings with a delimiter specified by the user. Delimiter any class that implements CharSequence may be sent to join().\" \/>\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-join-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String join() Method with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The java string join() method connects all strings with a delimiter specified by the user. Delimiter any class that implements CharSequence may be sent to join().\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-join-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-09-23T12:30:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-23T13:29:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String join() Method with Examples - TechVidvan","description":"The java string join() method connects all strings with a delimiter specified by the user. Delimiter any class that implements CharSequence may be sent to join().","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-join-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String join() Method with Examples - TechVidvan","og_description":"The java string join() method connects all strings with a delimiter specified by the user. Delimiter any class that implements CharSequence may be sent to join().","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-09-23T12:30:23+00:00","article_modified_time":"2024-09-23T13:29:32+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String join() Method with Examples","datePublished":"2024-09-23T12:30:23+00:00","dateModified":"2024-09-23T13:29:32+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/"},"wordCount":408,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.webp","keywords":["java string join()","Java String join() method","Java String join() method with example","java tutorials","Learn Java","string join() method in java","string join() with example"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/","name":"Java String join() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.webp","datePublished":"2024-09-23T12:30:23+00:00","dateModified":"2024-09-23T13:29:32+00:00","description":"The java string join() method connects all strings with a delimiter specified by the user. Delimiter any class that implements CharSequence may be sent to join().","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-join.webp","width":1200,"height":628,"caption":"java string join()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-join-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String join() 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\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89737","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=89737"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89737\/revisions"}],"predecessor-version":[{"id":447717,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89737\/revisions\/447717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447400"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}