{"id":1359,"date":"2017-05-06T10:19:34","date_gmt":"2017-05-06T10:19:34","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1359"},"modified":"2024-11-16T14:57:24","modified_gmt":"2024-11-16T14:57:24","slug":"what-is-stringtokenizer","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-stringtokenizer\/","title":{"rendered":"What is StringTokenizer?"},"content":{"rendered":"<h1>Overview<\/h1>\n<p><strong>StringTokenizer<\/strong> class is mostly used to break a string based upon a set of delimiter tokens. You can then call the Enumeration methods to loop through the elements.<\/p>\n<h2><strong>Class Diagram<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1064\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/StringTokenizer-Class-Heirarchy-1024x535-1.png\" alt=\"\" width=\"1024\" height=\"535\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/StringTokenizer-Class-Heirarchy-1024x535-1.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/StringTokenizer-Class-Heirarchy-1024x535-1-300x157.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/StringTokenizer-Class-Heirarchy-1024x535-1-768x401.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The set of delimiters are the characters that separate tokens. They can be specified either at creation time or on a per-token basis. A StringTokenizer object internally maintains a current position within the string to be tokenized. You can import the StringTokenizer from the java.util package as shown in below picture.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1581\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-StringTokenizer.png\" alt=\"Import StringTokenizer\" width=\"1269\" height=\"533\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>Note that StringTokenizer is a legacy class that is retained for some compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.<\/p>\n<p>Example of String.split method can be used to break up a string into its tokens:<\/p>\n<p>String[] result = \u201cSample string to test this snippet\u201d.split(\u201c\\\\s\u201d);<br \/>\nfor (int i=0; i &lt; result.length; i++)<br \/>\nSystem.out.println(result[i]);<\/p>\n<h3><strong>Sample Java Program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.StringTokenizer;\r\n\r\npublic class StringTokenizerExample {\r\n\r\npublic static void main(String... args) {\r\n \/\/ TODO Auto-generated method stub\r\n StringTokenizer tokenizer = new StringTokenizer(\"the quick brown fox jumps\"\r\n + \" over the lazy dog\");\r\n while (tokenizer.hasMoreElements()) {\r\n Object obj = tokenizer.nextElement();\r\n System.out.println(obj);\r\n }\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>Output of the program:<\/h3>\n<p>the<br \/>\nquick<br \/>\nbrown<br \/>\nfox<br \/>\njumps<br \/>\nover<br \/>\nthe<br \/>\nlazy<br \/>\ndog<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1359\" data-postid=\"1359\" class=\"themify_builder_content themify_builder_content-1359 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview StringTokenizer class is mostly used to break a string based upon a set of delimiter tokens. You can then call the Enumeration methods to loop through the elements. Class Diagram &nbsp; The set of delimiters are the characters that separate tokens. They can be specified either at creation time or on a per-token basis. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1359","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1359"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1359\/revisions"}],"predecessor-version":[{"id":21307,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1359\/revisions\/21307"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}