Lecture 10 Strings CSE 1322 4262018 10 1

  • Slides: 28
Download presentation
Lecture 10 Strings CSE 1322 4/26/2018 10 -1

Lecture 10 Strings CSE 1322 4/26/2018 10 -1

String class • A string is a sequence of characters stored in a certain

String class • A string is a sequence of characters stored in a certain address in memory. • Once created, it cannot be changed. It is immutable because the string class has no mutators methods. • It does not need to be explicitly instantiated (new is not needed) • Once created, the content does not change directly! • If we try to change the value, it will be saved to a new location in dynamic memory and the variable will point to it. 4/26/2018 10 -2

Main Ideas • String are objects, so they have a ton of methods! •

Main Ideas • String are objects, so they have a ton of methods! • • • Length – the number of characters in the string Substring – returns only part of a string Append – creates a new string made from other strings Upper/Lowercase Index. Of – returns the location of a character(s) • Most often, you can access them similar to arrays • String programming appears a lot in job interviews 4/26/2018 10 -3

Note • In Java, Strings start with a capital S • In C#, strings

Note • In Java, Strings start with a capital S • In C#, strings start with a lowercase s • For many of these examples, we don’t care and will use generic: S/string 4/26/2018 10 -4

Other note • Strings usually have a hidden character '�' that denotes the end

Other note • Strings usually have a hidden character '' that denotes the end of the string • It’s called a “null terminator” to create a “null terminated string” • This takes up one character of memory • Job interview question: how much memory needed to store the word “cat”? Answer: 4 characters (usually 8 bytes) 4/26/2018 10 -5

S/strings Strings are objects of the S/string class Examples: • A constant or literal:

S/strings Strings are objects of the S/string class Examples: • A constant or literal: e. g. "Hello" • A variables: S/string message = "Hello, World!"; • String length: int n = message. length(); • Empty string: "" 4/26/2018 10 -6

String Constant Pool • Java tries to be efficient and store (unchanging) strings in

String Constant Pool • Java tries to be efficient and store (unchanging) strings in a “pool” • Adds as much to the pool as possible s 1 String s 1 = new String (“A”); This adds a new string in memory AND to the string pool 4/26/2018 Memory - Heap String Object “A” The “pool” String Object “A” 10 -7

String Constant Pool • Java tries to be efficient and store (unchanging) strings in

String Constant Pool • Java tries to be efficient and store (unchanging) strings in a “pool” • Adds as much to the pool as possible s 1 Memory - Heap String Object “A” String s 1 = new String (“A”); String s 2 = “A”; The “pool” String Object “A” s 2 4/26/2018 10 -8

String Constant Pool • Java tries to be efficient and store (unchanging) strings in

String Constant Pool • Java tries to be efficient and store (unchanging) strings in a “pool” • Adds as much to the pool as possible s 1 String Object “A” String s 1 = new String (“A”); String s 2 = “A”; String s 3 = “A”; s 3 So only two objects in memory Memory - Heap The “pool” String Object “A” s 2 Note: this is somewhat esoteric, but Java employers like it 4/26/2018 10 -9

Concatenation • Use the + operator: S/string name = "Dave"; S/string message = "Hello,

Concatenation • Use the + operator: S/string name = "Dave"; S/string message = "Hello, " + name; // message is “Hello, Dave” • If one of the arguments of the + operator is a string, the other is converted to a string S/string a = "Agent"; int n = 7; String bond = a + n; // bond has the value Agent 7 4/26/2018 10 -10

Concatenation in Print Statements • Useful to reduce the number of Print instructions PRINT

Concatenation in Print Statements • Useful to reduce the number of Print instructions PRINT ("The total is "); PRINT (total); versus PRINT ("The total is " + total); 4/26/2018 10 -11

Converting between Strings and Numbers • Convert to number: int n = Integer. parse.

Converting between Strings and Numbers • Convert to number: int n = Integer. parse. Int(str); double x = Double. parse. Double(str); • Convert to string (easy!): String str = "" + n; // OR str = Integer. to. String(n); 4/26/2018 10 -12

Converting between Strings and Numbers • Convert to number: int n = int. Parse.

Converting between Strings and Numbers • Convert to number: int n = int. Parse. Int(str); double x = double. Parse(str); • Convert to string (easy!): string str = "" + n; // OR int many = 5; str = many. To. String(n); 4/26/2018 10 -13

Substrings Java H e l l o , 0 1 2 3 4 5

Substrings Java H e l l o , 0 1 2 3 4 5 6 W o r l d ! 7 8 9 10 11 12 Supply start and “past the end” position (be careful!) First position is at 0, last is 5 S/string greeting = "Hello, World!"; S/string sub = greeting. substring(0, 5); // sub is "Hello" 4/26/2018 10 -14

Substrings Java H e l l o , 0 1 2 3 4 5

Substrings Java H e l l o , 0 1 2 3 4 5 6 W o r l d ! 7 8 9 10 11 12 Supply start and “past the end” position (be careful!) First position is at 7, last is 12 S/string greeting = "Hello, World!"; S/string sub = greeting. substring(7, 12); // sub is "World" 4/26/2018 10 -15

Length Property • Strings usually have either: • A getter (C#) like public int

Length Property • Strings usually have either: • A getter (C#) like public int Length { get; } • A method (Java) like length(); • These return the number of characters in the string. • The Length property returns the number of Char objects in this instance, not the number of Unicode characters. Example: string h= “hello”; int len = h. Length; // len has a value of 5 4/26/2018 10 -16

Challenge string s ="Agent"; • What is the effect of the assignment s =

Challenge string s ="Agent"; • What is the effect of the assignment s = s + s. Length; string river ="Mississippi"; • What is the value of river. Substring(1, 2)? • What is the value of river. Substring(2, river. Length-3)? • What is the value of river. Substring(1)? 4/26/2018 10 -17

Answers • Agent 5 • i • ssiss • ississippi 4/26/2018 10 -18

Answers • Agent 5 • i • ssiss • ississippi 4/26/2018 10 -18

Additional methods • To. Upper/to. Upper. Case and To. Lower/to. Lower. Case • index.

Additional methods • To. Upper/to. Upper. Case and To. Lower/to. Lower. Case • index. Of() • char. At (Java) and array access (C#) of individual characters • Replace/replace (Java) • Trim/trim – a *wonderful* way to get rid of leading/trailing whitespace! 4/26/2018 10 -19

To upper and lower case public string To. Lower() Return Value: A string in

To upper and lower case public string To. Lower() Return Value: A string in lowercase. public string To. Upper() Return Value: A string in uppercase. Example: string my. String = “good luck”; my. String = my. String. To. Upper(); //my. String now has the value “GOOD LUCK” 4/26/2018 10 -20

To upper and lower case public string to. Lower. Case() Return Value: A string

To upper and lower case public string to. Lower. Case() Return Value: A string in lowercase. public string to. Upper. Case() Return Value: A string in uppercase. Example: String my. String = “good luck”; my. String = my. String. to. Upper. Case(); //my. String now has the value “GOOD LUCK” 4/26/2018 10 -21

Index. Of methods public int Index. Of( char value ) The zero-based index position

Index. Of methods public int Index. Of( char value ) The zero-based index position of value if that character is found, or -1 if it is not. public int Index. Of( string value) The zero-based index position of value if that string is found, or -1 if it is not. string my. String= “hello world”; int e_index=my. String. Index. Of(‘e’); // e_index= 1 int or_index= my. String. Index. Of(“or”); //or_index=7 4/26/2018 10 -22

index. Of methods public int index. Of( char value ) The zero-based index position

index. Of methods public int index. Of( char value ) The zero-based index position of value if that character is found, or -1 if it is not. public int index. Of( string value) The zero-based index position of value if that string is found, or -1 if it is not. String my. String= “hello world”; int e_index=my. String. index. Of(‘e’); // e_index= 1 int or_index= my. String. index. Of(“or”); //or_index=7 4/26/2018 10 -23

Accessing Individual Characters • Java - use char. At(int x); String bob = "Bob";

Accessing Individual Characters • Java - use char. At(int x); String bob = "Bob"; System. out. println (bob. char. At(2)); // Prints b • C# - use array indices string bob = "Bob"; Console. Write. Line (bob[2]); // Prints b 4/26/2018 10 -24

Trimming Extra Whitespace (super common) • Java - use trim(); String bob = "

Trimming Extra Whitespace (super common) • Java - use trim(); String bob = " t Bob "; System. out. println (bob. trim()); // Prints “Bob” – no spaces • C# - use array indices string bob = " t Bob "; Console. Write. Line (bob. Trim()); // Prints “Bob” 4/26/2018 10 -25

Final Notes • A null string is not the same thing as an empty

Final Notes • A null string is not the same thing as an empty string: S/string s 1 = null; S/string s 2 = “”; • s 1 is “dead” and points to null • s 2 points to something “alive” that contains no text • Often, you can convert a string to/from a character array (char[]). Quite useful for manipulation! 4/26/2018 10 -26

In-class Problems • Write the code to: See if the word “Bubba” appears in

In-class Problems • Write the code to: See if the word “Bubba” appears in a string Count how many vowels are in a string Reverse a string Determine if a string is a palindrome Check to see if the first letter of each word is capitalized Replaces all tabs 't’ with newlines 'n’ Adds one to each letter (e. g. ‘a’ becomes ‘b’, ‘b’ becomes ‘c’) so that “CSE 1321” would become “DTF 2432” • Print all possible substrings of a given string (that’s a fun one!) • • 4/26/2018 10 -27

Frequently Used String Methods • https: //www. tutorialspoint. com/java_strings. htm • https: //www. completecsharptutorial.

Frequently Used String Methods • https: //www. tutorialspoint. com/java_strings. htm • https: //www. completecsharptutorial. com/csharp-articles/csharpstring-function. php 4/26/2018 10 -28