{"id":3394,"date":"2024-02-08T13:22:41","date_gmt":"2024-02-08T12:22:41","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3394"},"modified":"2024-12-22T20:26:15","modified_gmt":"2024-12-22T19:26:15","slug":"5-3-string-values","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3394","title":{"rendered":"5.3 String values"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3394&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3394&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In the first version of class <code>Account<\/code>, the owner of the account was represented by a <code>String<\/code> data item. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>var<\/strong> String):\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n   -\"- <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The specification <code><strong>var<\/strong> String<\/code> in the declaration of <code>owner<\/code>, specifies that <code>owner<\/code> may hold string values, and <code>String<\/code> is the type of <code>owner<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The type <code>String<\/code> is usually not considered a primitive value type. The reason is that the primitive values may be represented in a fixed number of bytes in the computer, whereas a <code>String<\/code> value may need a variable number of bytes for its representation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A string value is a sequence of characters as opposed to char values, which are single characters. A string value may thus consist of zero or more characters. In the program text, a string value is enclosed in double quotes (&#8220;) and special characters are preceded by a slash (\\). Example are shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"John Smith\"       \n\"Hello world\\n\".          -- a newline is represented by \\n\n\"a + b * (c +111)\"\n\"He said: \\\"Go away!\\\"\"   -- the character \" is represented by \\\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>length<\/code> of a <code>String<\/code> returns the number of characters in the <code>String<\/code>, and the method <code>get<\/code> returns the character at a given position in a <code>String<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following example shows how to get the characters in a given <code>String<\/code> and print them on the console. <code>s: <strong>val<\/strong> \"Hello world\" <\/code>specifies that <code>s<\/code> has the value <code>\"Hello world\"<\/code> during the whole program execution.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s: <strong>val<\/strong> \"Hello world\"\nfor (1):to S.length :repeat\n   console.print(S.get&#91;inx])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">The default value for a <code>String<\/code> variable is the empty string (<code>\"\"<\/code>).<\/mark><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>get<\/code> method<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next example shows how to search for an occurrence of a given character in a <code>String<\/code>. The method assumes that the <code>aName<\/code> holds a name consisting of a first name and a surname, separated by blanks<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aName: <strong>val<\/strong> \"John Smith\"\nfirstSurname:\n   i: <strong>var<\/strong> integer\n   i := 1\n   while (aName.get&#91;i] &lt;&gt; ' ') :repeat\n       console.print(aName.get&#91;i])\n       i := i + 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here we use a while-loop which executes a sequence of statements as long as the condition is <code>True<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>aName.get[i] &lt;&gt; ' '<\/code> is the condition that is evaluated before each iteration of the loop. As long as this condition is true, the statements <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.print(aName.get&#91;i])\ni := i + 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">are executed. The operator <code>&lt;&gt;<\/code> means &#8220;not equal&#8221;, which means that the expression <code>aName.get[i] &lt;&gt; ' '<\/code> is true if <code>aName.get[i]<\/code> is not equal to the character <code>' '<\/code> (blank).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As can been seen, <code>i = 1<\/code> in the first iteration and the condition is thus <code>aName.get[1] &lt;&gt; ' '<\/code>, which evaluates to <code>true<\/code> since <code>aName[1] = 'J'<\/code> (the first char in <code>aName<\/code>) which is not equal to the the char <code>' '<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the second iteration, <code>i = 2<\/code> and the condition again evaluates to <code>true<\/code> since  <code>aName[2] = 'o'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally when <code>i = 5<\/code>, the condition evaluates to <code>False<\/code>, since <code>aName[5] = ' '<\/code>, and the while-loop terminates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">String operators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A number of operators are available on <code>String<\/code> values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operator <code>'+'<\/code> concatenates two <code>String<\/code> values and return a new <code>String<\/code> value. This is illustrated by the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>S1,S2: <strong>var<\/strong> String\nS1 := \"John\" + \" Smith\"\nS2 := \"Hello \" + S1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>\"John\" + \" Smith\" <\/code>evaluates to the <code>String<\/code> <code>\"John Smith\"<\/code>, which is then assigned to <code>S1<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>\"Hello \" + S1<\/code> evaluates to the <code>String<\/code> <code>\"Hello John Smith\"<\/code>, which is assigned to <code>S2<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operator <code>'='<\/code> compares two <code>String<\/code> values, and evaluates to <code>true<\/code> if the two <code>Strings<\/code> are identical and <code>false<\/code> if they differ. Consider the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>S1,S2: <strong>var<\/strong> String\nS1 := \"John\"\nS2 := \"Liza\"\nS1 = S2  -- false\nS1 &lt;&gt; S2 -- true\nS1 = \"John\" -- true\nS1 = \"john\" -- false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that <code>S1 = \"john\"<\/code> evaluates to <code>false<\/code> since the upper-case letter <code>'J'<\/code> differs from the lower-case letter <code>'j'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operator <code>'&lt;='<\/code> compares two <code>String<\/code> values according to the numerical value of the characters in the strings. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Immutability<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>String <\/code>value is immutable, which means that it cannot be changed. There is thus no <code>put<\/code> method on <code>String<\/code> values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=3394\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In the first version of class Account, the owner of the account was represented by a String data item. The specification var String in the declaration of owner, specifies that owner may hold string values, and String is the type of owner. The type String is usually not considered a primitive value type. The reason [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2270,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3394","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3394"}],"version-history":[{"count":33,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3394\/revisions"}],"predecessor-version":[{"id":10795,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3394\/revisions\/10795"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2270"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}