String Data Type

String Data Type

String Data Type Recall that most programming languages have primitive data types. These are simple to describe and store in RAM, such as integer, character, and boolean. A string is a collection of one or more characters. A string is a complex data type, since it is constructed from multiple primitive data elements. Declaring Strings & Assigning Values Turing Java var str1 : string := "hello" String str1 = "hello"; var str2 : string String str2; str2 := "goodbye" str2 = "goodbye"; put str1 println(str1); put str2 println(str2); put "literal" println("literal"); Strings as Collections of Characters Turing Java Start counting at one: Start counting at zero: "hello" "hello" 12345 01234 "goodbye" "goodbye" 1234567 0123456 Length of a String (number of characters) Turing Java var str1 : string String str1; var len : int int len; str1 := "hello" str1 = "hello"; len := length(str1) len = str1.length(); put "String is: ", str1 println(str1); put "Length: ", len println(len); Comparing Strings Turing Java var str1, str2 : string String str1, str2; var same : boolean boolean same; str1 := "hello" str1 = "hello"; str2 := "goodbye" str2 = "goodbye"; same := str1 = str2 same = str1.equals(str2); if (str1 = str2) if (str1.equals(str2)) put "Same!" println("Same!"); end if end if Concatenate (join) Strings Java Turing String str1, str2, str3; var str1, str2, str3 : string str1 = "hello"; str1 := "hello" str2 = "goodbye"; str2 := "goodbye" str3 = str1 + str2; str3 := str1 + str2 // OR str3 = str1.concat(str2); String to Upper Case Turing Java var str1, str2 : string String str1, str2; str1 := "Hello" str1 = "hello"; % 12345 // 01234 str2 := Str.Upper(str1) str2 = str1.toUpperCase(); println(str2); put str2 // output is "HELLO" % output is "HELLO" String to Lower Case Turing Java var str1, str2 : string String str1, str2; str1 := "Hello" str1 = "HelLo"; % 12345 // 01234 str2 := Str.Lower(str1) str2 = str1.toLowerCase(); println(str2); put str2 // output is "hello" % output is "hello" Convert a String to a Number Turing Java var str1 : string String str1; var num : int int num; str1 := "123" str1 = "123"; num := strint(str1) num=Integer.parseInt(str1); println(num); // 123 put num % 123 println(num+1); // 124 put num+1 % 124 Test if String to Number Possible Turing var str1 : string var num : int str1 := "123" if strintok(str1) then num := strint(str1) put num % 123 put num+1 % 124 end if Single Element (character) from a String Turing Java var str1 : string String str1; var ch : char char ch; str1 := "hello" str1 = "hello"; % 12345 // 01234 ch := str1(2) % 'e' ch = str1.charAt(1); put ch println(ch); put str1(2) println(str1.charAt(1)); Traverse String (visit one character at a time) Java Turing String str1; var str1 : string char ch; var ch : char str1 = "hello"; str1 := "hello" // 01234 % 12345 for (int i=0; i<str1.length(); i++) for i : 1 .. length(str1) { ch := str1(i) ch = str1.charAt(i); put ch println(ch); end for } Substring by Traversing (visit one character at a time) Turing Java var str1, str2 : string String str1, str2; var ch : char char ch; str1 := "hello" str1 = "hello"; % 12345 // 01234 str2 := "" % null str str2 = ""; // null str for i : 2 .. 4 for (int i=1; i<4; i++) ch := str1(i) { str2 := str2 + ch ch = str1.charAt(i); end for str2 = str2 + ch; } put str2 println(str2); Substring using Functions Turing Java var str1, str2 : string String str1, str2; str1 := "hello" str1 = "hello"; % 12345 // 01234 str2 := str1(2..4) str2 = st1.substring(1,4); put str2 println(str2); % output is "ell" // output is "ell" Search for Char by Traversing (visit one character at a time) Turing Java var str1 : string var ch, find : char String str1; char ch, find; str1 := "hello" % 12345 str1 = "hello"; find := 'l' // 01234 find = 'l'; for i : 1 .. length(str1) for (int i=1; i<str1.length(); i++) ch := str1(i) { if (ch = find) then ch = str1.charAt(i); if (ch==find) put find, " at ", i { end if println(""+find+" at "+i); } end for } Search for Specific Character Location within String Turing Java var str1 : string String str1; var loc : int int loc; str1 := "Hello" str1 = "hello"; % 12345 // 01234 loc := index(str1,'l') loc=str1.indexOf('l'); put loc println(loc); % output is 3 // output is 2 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us