{"id":68133,"date":"2006-01-20T14:43:00","date_gmt":"2006-01-20T14:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/20\/how-can-i-extract-a-series-of-characters-found-in-the-middle-of-a-string\/"},"modified":"2006-01-20T14:43:00","modified_gmt":"2006-01-20T14:43:00","slug":"how-can-i-extract-a-series-of-characters-found-in-the-middle-of-a-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-extract-a-series-of-characters-found-in-the-middle-of-a-string\/","title":{"rendered":"How Can I Extract a Series of Characters Found in the Middle of a String?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I have a series of strings that look something like this: <B>xxxxyyyyyyxx<\/B>. How can I extract just the y\u2019s?<BR><BR>&#8212; JB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JB. And, yes, it <I>is<\/I> a Friday. But, hey, it\u2019s just a coincidence that we happened to pick an easy question on a Friday, the day we\u2019re itching to get out of here and go home and spend the weekend watching football and basketball. Besides, we feel it\u2019s important to tackle these easier questions from time-to-time to better help those readers who are new to scripting.<\/P>\n<P>Boy, that\u2019s so convincing we almost believe it ourselves.<\/P>\n<P>But enough about us. Let\u2019s talk about the code instead:<\/P><PRE class=\"codeSample\">strPhrase = &#8220;xxxxyyyyyyxx&#8221;\nstrExtract = Mid(strPhrase, 5, 6)\nWscript.Echo strExtract\n<\/PRE>\n<P>As you can see, there\u2019s not much to this script. We start out by assigning the string <I>xxxxyyyyyyxx<\/I> to a variable named strPhrase. As you noted, there\u2019s a pattern to your series of strings: each string starts out with four characters we don\u2019t care about, followed by six characters we <I>do<\/I> care about, followed by two other characters we don\u2019t care about. Our job is simple: grab those six characters out of the middle of the string.<\/P>\n<P>So how do we do that? We do that by using the <B>Mid<\/B> function:<\/P><PRE class=\"codeSample\">strExtract = Mid(strPhrase, 5, 6)\n<\/PRE>\n<P>As you can see, we pass the Mid function three parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strPhrase<\/B>. This is the string we want to parse.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>5<\/B>. This is the character position we want to start grabbing characters from. We want to start with character 5 (skipping the first 4 characters), so we pass 5 as the second parameter.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>6<\/B>. The number of characters we want to grab.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In other words, starting with character 5, we want to grab 6 characters out of the middle of the string and store them in a variable named strExtract. And what happens when we echo back the value of strExtract? Why, we get this, of course:<\/P><PRE class=\"codeSample\">yyyyyy\n<\/PRE>\n<P>Keep in mind that we\u2019re dealing solely with numbers here; we\u2019re not looking for the actual string <I>yyyyyy<\/I>. With that in mind, consider this script:<\/P><PRE class=\"codeSample\">strPhrase = &#8220;abcdcreateef&#8221;\nstrExtract = Mid(strPhrase, 5, 6)\nWscript.Echo strExtract<\/p>\n<p>strPhrase = &#8220;ghijdeletekl&#8221;\nstrExtract = Mid(strPhrase, 5, 6)\nWscript.Echo strExtract\n<\/PRE>\n<P>Here\u2019s what we get when we run <I>this<\/I> script:<\/P><PRE class=\"codeSample\">create\ndelete\n<\/PRE>\n<P>Again, we started at character 5, and grabbed a total of 6 characters. Simple, huh?<\/P>\n<P>And so it\u2019s Friday and we\u2019ve already finished the <I>Hey, Scripting Guy!<\/I> column. Gee, what do you think we should do now \u2026?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a series of strings that look something like this: xxxxyyyyyyxx. How can I extract just the y\u2019s?&#8212; JB Hey, JB. And, yes, it is a Friday. But, hey, it\u2019s just a coincidence that we happened to pick an easy question on a Friday, the day we\u2019re itching to get out [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[174,3,4,21,5],"class_list":["post-68133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a series of strings that look something like this: xxxxyyyyyyxx. How can I extract just the y\u2019s?&#8212; JB Hey, JB. And, yes, it is a Friday. But, hey, it\u2019s just a coincidence that we happened to pick an easy question on a Friday, the day we\u2019re itching to get out [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}