Download presentation
Presentation is loading. Please wait.
1
Lecture 10 Strings CSE 1322 4/26/2018
2
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
3
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 IndexOf – 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
4
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
5
Other note Strings usually have a hidden character '\0' 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
6
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
7
String Constant Pool Java tries to be efficient and store (unchanging) strings in a “pool” Adds as much to the pool as possible String s1 = new String (“A”); This adds a new string in memory AND to the string pool Memory - Heap s1 String Object “A” The “pool” String Object “A” 4/26/2018
8
String Constant Pool Java tries to be efficient and store (unchanging) strings in a “pool” Adds as much to the pool as possible String s1 = new String (“A”); String s2 = “A”; Memory - Heap s1 String Object “A” The “pool” String Object “A” s2 4/26/2018
9
String Constant Pool Java tries to be efficient and store (unchanging) strings in a “pool” Adds as much to the pool as possible String s1 = new String (“A”); String s2 = “A”; String s3 = “A”; So only two objects in memory Memory - Heap s1 String Object “A” The “pool” String Object “A” s3 s2 Note: this is somewhat esoteric, but Java employers like it 4/26/2018
10
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 Agent7 4/26/2018
11
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
12
Converting between Strings and Numbers
Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(str); Convert to string (easy!): String str = "" + n; // OR str = Integer.toString(n); 4/26/2018
13
Converting between Strings and Numbers
Convert to number: int n = int.ParseInt(str); double x = double.Parse(str); Convert to string (easy!): string str = "" + n; // OR int many = 5; str = many.ToString(n); 4/26/2018
14
Substrings Java First position is at 0, last is 5
H e l o , W r d ! 1 2 3 4 5 6 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
15
Substrings Java First position is at 7, last is 12
H e l o , W r d ! 1 2 3 4 5 6 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
16
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
17
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
18
Answers Agent5 i ssiss ississippi 4/26/2018
19
Additional methods ToUpper/toUpperCase and ToLower/toLowerCase
indexOf() charAt (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
20
To upper and lower case public string ToLower() Return Value: A string in lowercase. public string ToUpper() Return Value: A string in uppercase. Example: string myString = “good luck”; myString = myString.ToUpper(); //myString now has the value “GOOD LUCK” 4/26/2018
21
To upper and lower case public string toLowerCase() Return Value: A string in lowercase. public string toUpperCase() Return Value: A string in uppercase. Example: String myString = “good luck”; myString = myString.toUpperCase(); //myString now has the value “GOOD LUCK” 4/26/2018
22
IndexOf methods public int IndexOf( char value )
The zero-based index position of value if that character is found, or -1 if it is not. public int IndexOf( string value) The zero-based index position of value if that string is found, or -1 if it is not. string myString= “hello world”; int e_index=myString.IndexOf(‘e’); // e_index= 1 int or_index= myString.IndexOf(“or”); //or_index=7 4/26/2018
23
indexOf methods public int indexOf( char value )
The zero-based index position of value if that character is found, or -1 if it is not. public int indexOf( string value) The zero-based index position of value if that string is found, or -1 if it is not. String myString= “hello world”; int e_index=myString.indexOf(‘e’); // e_index= 1 int or_index= myString.indexOf(“or”); //or_index=7 4/26/2018
24
Accessing Individual Characters
Java - use charAt(int x); String bob = "Bob"; System.out.println (bob.charAt(2)); // Prints b C# - use array indices string bob = "Bob"; Console.WriteLine (bob[2]); // Prints b 4/26/2018
25
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.WriteLine (bob.Trim()); // Prints “Bob” 4/26/2018
26
Final Notes A null string is not the same thing as an empty string:
S/string s1 = null; S/string s2 = “”; s1 is “dead” and points to null s2 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
27
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
28
Frequently Used String Methods
string-function.php 4/26/2018
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.