{"id":211,"date":"2010-04-28T20:04:23","date_gmt":"2010-04-28T20:04:23","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=211"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"working-with-strings","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/strings\/working-with-strings","title":{"rendered":"Working With Strings"},"content":{"rendered":"<h4>\nString Special Characters<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nsTemp += &quot;\\r\\n&quot;\t\t\/\/Add a newline\r\nString ^sTemp =  = &quot;\\x7&quot;;\t\/\/ &lsquo;\\x&rsquo; means &lsquo;0x&rsquo; and the 1 of 2 digits that follow are a hex value for the character requried\r\n\r\n\tnewline( LF)\t\t\\n\t\thorizontal tab (TAB)\t\\t\r\n\tvertical tab\t\t\\v\t\tbackspace\t\t\\b\r\n\tcarriage return (CR)\t\\r\t\tformfeed\t\t\\f\r\n\talert (bell)\t\t\\a\t\tbackslash\t\t\\\\\r\n\tquestion mark\t\t\\?\t\tsingle quote\t\t\\&#39;\r\n\tdouble quote\t\t\\&quot;\t\tnull (NULL)\t\t\\0\r\n<\/code><\/pre>\n<p>\nYou can do this:<br \/>\nMyString = &quot;\\x1b&quot; &quot;N&quot; &quot;\\x1&quot;; \/\/N&lt;1&gt;\n<\/p>\n<h4>\nLength<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tint Count = myString-&gt;Length;\r\n<\/code><\/pre>\n<h4>\nRemoving spaces (or other characters) from beginning and end of a string<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMyString = MyString-&gt;Trim();\r\n<\/code><\/pre>\n<h4>\nRead Characters Within A String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString ^MyString;\r\n\r\n\tChar a = Convert::ToChar(MyString[3]);\r\n\r\n\tif (Convert::ToChar(MyString[1]) != &#39;.&#39;)\r\n<\/code><\/pre>\n<h4>\nFind First Instance Of<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tIndex = TempString-&gt;IndexOf(&quot;\\\\&quot;);\r\n<\/code><\/pre>\n<h4>\nFind Last Instance Of<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tIndex = TempString-&gt;LastIndexOf(&quot;\\\\&quot;);\r\n<\/code><\/pre>\n<h4>\nGet string from character position # (Remove text from beginning or end of string)<br \/>\n<\/h4>\n<h5>\nGetting beginning of a string<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif (TempString-&gt;LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\tTempString = TempString-&gt;Substring(0, (TempString-&gt;LastIndexOf(&quot;\\\\&quot;)));\r\n<\/code><\/pre>\n<h5>\nGetting end of string<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif (TempString-&gt;LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\tTempString = TempString-&gt;Substring(TempString-&gt;LastIndexOf(&quot;\\\\&quot;) + 1);\r\n<\/code><\/pre>\n<h4>\nGet Characters Between Markers Within String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString ^StartAfterChars;\r\n\tString ^EndBeforeChars;\r\n\tint Start;\r\n\tint Length;\r\n\r\n\tStartAfterChars = &quot;?a=&quot;;\r\n\tEndBeforeChars = &quot;&amp;b=&quot;;\r\n\tStart = sTemp-&gt;IndexOf(StartAfterChars) + StartAfterChars-&gt;Length;\r\n\tLength = sTemp-&gt;IndexOf(EndBeforeChars) - Start;\r\n\tif ((Start &gt; 0) &amp;&amp; (Length &gt; 0))\r\n\t\tUserId = sTemp-&gt;Substring(Start, Length);\r\n\telse\r\n\t\tUserId = &quot;&quot;;\r\n<\/code><\/pre>\n<h4>\nInsert Into String<br \/>\n<\/h4>\n<pre>\r\n<code>\tmyString-&gt;Insert(0, &quot;1234&quot;);\t\/\/Insert at character 0<\/code><\/pre>\n<h4>\nReplace Characters In String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tmyString-&gt;Replace(&quot;.&quot;, &quot;!&quot;);\t\/\/Replace all . with !\r\n\tsTemp = sTemp-&gt;Replace(&#39;A&#39;, &#39;B&#39;);\t\/\/Replace a with B\r\n\tsTemp = sTemp-&gt;Replace(&quot;RemoveMe&quot;, &quot;ReplaceWitMe&quot;);\r\n<\/code><\/pre>\n<h4>\nAppend To End Of String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tmyString-&gt;Append(&quot;1234&quot;);\r\n\tmyString-&gt;Append(gcnew array{&#39;+&#39;, &#39;+&#39;});\r\n<\/code><\/pre>\n<h4>\nDoes string contain #<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (MyString-&gt;Contains(&quot;ABCDEFG&quot;))\r\n<\/code><\/pre>\n<h4>\nValidating Text String Entry<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMyString = MyString-&gt;Trim();\t\t\/\/Remove leading or trailing spaces\r\n\tif (myString-&gt;Length &gt; 30)\r\n\t\tmyString = myString-&gt;Substring(0, 29);\r\n\r\n\t\/\/Use these to force case:-\r\n\t\/\/MyString = MyString-&gt;ToLower\r\n\t\/\/MyString = MyString-&gt;ToUpper\r\n<\/code><\/pre>\n<h4>\nValidating Numeric String Entry<br \/>\n<\/h4>\n<h5>\nCheck for integer value entered<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tString ^ValueEntered;\r\n\tValueEntered = txtMyTextBox-&gt;Text;\r\n\tValueEntered= ValueEntered-&gt;Trim();\t\t\t\t\/\/Remove leading and trailing spaces\r\n\tif (\r\n\t(ValueEntered-&gt;Length &lt; 1) ||\r\n\t(!System::Text::RegularExpressions::Regex::IsMatch(ValueEntered, &quot;^[0-9]*$&quot;))\r\n\t)\r\n\t\t\/\/Value is not numeric\r\n<\/code><\/pre>\n<h5>\nCheck for general numberic value entered<br \/>\n<\/h5>\n<p>\nUse one of the solutions below as the best regex expression I have found is:\n<\/p>\n<pre>\r\n<code>\t\t&quot;^[-0-9]*.[.0-9].[0-9]*$&quot;<\/code><\/pre>\n<p>\nThis is greate if you ensure the string as at least 3 digits long by adding leading zero&#39;s, but it will let through multiple &#39;.&#39; being entered.\n<\/p>\n<h5>\nAn alternative solution uses a try, catch approach which isn&#39;t nearly as nice<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nInt32 myInt = 0;\r\ntry\r\n{\r\n\tmyInt = System::Convert::ToInt32 (myTextBox-&gt;Text);\r\n}\r\ncatch (System::FormatException * pEx)\r\n{\r\n\tmyInt = 0; \/\/ or another default value\r\n}\r\ncatch (System::OverflowException * pEx)\r\n{\r\n\tmyInt = 0; \/\/ or another default value\r\n}\r\n<\/code><\/pre>\n<h5>\nYou could use the VB IsNumeric function by doing this, but not a &#39;nice&#39; approach as you have to reference another dll<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t#using\r\n\t...\r\n\t...\r\n\tif ( Microsoft::VisualBasic::Information::IsNumeric ( myTextBox-&gt;Text ) )\r\n\t   myInt = System::Convert::ToInt32 ( myTextBox-&gt;Text );\r\n\telse\r\n\t   MessageBox::Show ( &quot;Value not numeric&quot; );\r\n<\/code><\/pre>\n<h4>\nThere are lots of other tests in the String class<br \/>\n<\/h4>\n<p style=\"padding-left: 30px;\">\nIndexOf Overloaded. Reports the index of the first occurrence of a String, or one or more characters, within this string.<br \/>\nIndexOfAny Overloaded. Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters.<br \/>\nLastIndexOf Overloaded. Reports the index position of the last occurrence of a specified Unicode character or String within this instance.<br \/>\nLastIndexOfAny Overloaded. Reports the index position of the last occurrence in this instance of one or more characters specified in a Unicode array.\n<\/p>\n<p style=\"padding-left: 30px;\">\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String Special Characters sTemp += &quot;\\r\\n&quot; \/\/Add a newline String ^sTemp = = &quot;\\x7&quot;; \/\/ &lsquo;\\x&rsquo; means &lsquo;0x&rsquo; and the 1 of 2 digits that follow are a hex value for the character requried newline( LF) \\n horizontal tab (TAB) \\t vertical tab \\v backspace \\b carriage return (CR) \\r formfeed \\f alert (bell) \\a [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/211\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}