110 likes | 300 Views
Strings. String. Anyone recognize this string?. GACGGGCTCTGACCCCCTTCGCG. List other places where you use strings regularly. String. A string (text) is made up of keyboard (and other) possible symbols. Strings in Programming. Most programming languages support String data.
E N D
String Anyone recognize this string? GACGGGCTCTGACCCCCTTCGCG List other places where you use strings regularly.
String A string (text) is made up of keyboard (and other) possible symbols.
Strings in Programming Most programming languages support String data. We are going to examine a relatively standard string notation, that works in the following languages: Perl Python Javascript CAUTION: Syntax matters in these languages!
Strings in Programming A literal is enclosed within "…" "The quick brown fox jumps over a lazy dog." "Computational Thinking" "University of Wisconsin – La Crosse" Any string can be assigned to one or more variable(s). Syntax: variable = stringExpression; gettysburg = "Four score and seven years ago ... "; lyric1 = "We are in the crowd, we’re c-comin’ out"; bestActor = "Jack Nicholson";
Strings Concatenation Two strings can be joined (concatenated) using + legalAssoc= "para"+ "legal"; combined = "social" + "media " + "website"; street = "1600 Pennsylvania Ave."; city = "Washington, DC"; zip = "20006”; space = ""; address = street + space + city + " " + zip;
String Length Every string has a length – the count of its characters. bestActor = "Jack Nicholson"; bestActor.length is 14 lyric1 = "We are in the crowd, we’re c-comin’ out"; lyric1.length is 39 gettysburg = "Four score and seven years ago ... "; gettysburg.length is 35
Character Positions Every string consists of a sequence of characters that are numbered by position. Each position has an integer index. The first (leftmost) position is zero (0). groundBeef = "hamburger"; 0 1 2 3 4 5 6 7 8
Indexing a String The following is a formula to retrieve a single character from a string: strExpr[index] where strExpris any valid string expression and indexis an integer expression with value in the range from 0 through strExpr.length. groundBeef = "hamburger"; 0 1 2 3 4 5 6 7 8 oneLetter = groundBeef[6]; curious = groundBeef[0] + groundBeef[2] + groundBeef[2]; parent = groundBeef[2] + "home"[1] + groundBeef[3-1]; lastLetter = groundBeef[groundBeef.length-1];
Substring The following is a formula to retrieve a substring: strExpr.substring( index1, index2 ) where strExpris any valid string expression and index1≤ index2 are integer expressions with values in the range from 0 through strExpr.length. groundBeef = "hamburger"; 0 1 2 3 4 5 6 7 8 kwikTripWord = groundBeef.substring(4,8); growl = groundBeef.subtring(6,groundBeef.length); porker= groundBeef.substring(0,groundBeef.length-6);
More Strings Manipulation hmm = "If the facts don't fit the theory, change the facts. "; blank = hmm[2]; lastSymbol = hmm[ hmm.length-1 ]; diem = "Carpe per diem – seize the check. "; keyword = diem.substring(6,9); expiration= diem.substring(10,13); longWord= "Supercalifragilisticexpialidocious"; word1= longWord.substring(1,5); word2= longWord.substring(9,15); phrase = longWord[16] + word1 + " " + word2 + ". ";