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 an immutable object because the string class has no mutators methods. Unlike other objects, it does not need to be explicitly instantiated After being assigned once, the content of the variable does not change directly – if we try to change the value, it will be saved to a new location in the dynamic memory and the variable will point to it. 4/26/2018
3
Strings java Strings are objects of the String class
String constants or literals: String variables: String length: Empty string: "Hello, World!" String message = "Hello, World!"; int n = message.length(); "" 4/26/2018
4
Strings C# Strings are objects of the string class
String constants or literals: String variables: String length: Empty string: "Hello, World!" string message = "Hello, World!"; int n = message.Length; "" 4/26/2018
5
String Object vs String Reference
String Constant Pool 4/26/2018
6
String Object vs String Reference
String Constant Pool 4/26/2018
7
Concatenation Use the + operator:
If one of the arguments of the + operator is a string, the other is converted to a string String name = "Dave"; String message = "Hello, " + name; // message is "Hello, Dave" String a = "Agent"; int n = 7; String bond = a + n; // bond is Agent7 4/26/2018
8
Concatenation Use the + operator:
If one of the arguments of the + operator is a string, the other is converted to a string string name = "Dave"; string message = "Hello, " + name; // message is "Hello, Dave" string a = "Agent"; int n = 7; String bond = a + n; // bond is Agent7 4/26/2018
9
Concatenation in Print Statements
Useful to reduce the number of System.out.print instructions versus System.out.print("The total is "); System.out.println(total); System.out.println("The total is " + total); 4/26/2018
10
Concatenation in Print Statements
Useful to reduce the number of Console.Write instructions versus Console.Write("The total is "); Console.WriteLIne(total); Console.WriteLine("The total is " + total); 4/26/2018
11
Converting between Strings and Numbers
Convert to number: Convert to string: int n = Integer.parseInt(str); double x = Double.parseDouble(x); String str = "" + n; str = Integer.toString(n); 4/26/2018
12
Converting between Strings and Numbers
Convert to number: Convert to string: int n = int.ParseInt(str); double x = double.Parse(x); String str = "" + n; int many = 5; string xyz = many.ToString(); 4/26/2018
13
Substrings Java Supply start and “past the end” position
String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello" Supply start and “past the end” position First position is at 0 4/26/2018
14
Substrings Java Substring length is “past the end” - start
String greeting = "Hello, World!"; String sub = greeting.substring(7, 12); // sub is “World" Substring length is “past the end” - start 4/26/2018
15
Substrings C# Supply start and the number of characters desired
string greeting = "Hello, World!"; string sub = greeting.substring(0, 5); // sub is "Hello" Supply start and the number of characters desired First position is at 0 4/26/2018
16
Substrings Java String greeting = "Hello, World!"; String sub = greeting.substring(7, 12); // sub is “World" 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)? 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 Agent5 i ssissip ississippi
4/26/2018
19
4/26/2018
20
Length Property public int Length { get; } The number of characters in the current string. Remarks 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
21
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
22
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
23
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
24
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
25
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
26
Problem-1: Reverse a String (Using a Char Array)
4/26/2018
27
Problem-1: Reverse a String (in place)
4/26/2018
28
Problem-1: Reverse a String (in place)
public class StringReverseInPlaceEx1 { public static void main(String[] args) { String a = “INFORMATION"; System.out.println(reverse(a)); //NOITAMROFNI } private static String reverse(String a) { char[] ca = a.toCharArray(); int start = 0 ; int end = a.length()-1; while(end > start) { swap(ca,start,end); start++; end--; }//while return new String(ca); private static void swap(char[] ca, int start, int end) { char t = ca[start]; ca[start] = ca[end]; ca[end] = t ; 4/26/2018
29
Problem-1: Reverse a String (in place)
public class StringReverseInPlaceEx2 { public static void main(String[] args) { String a = "INFORMATION"; System.out.println(StringReverseInPlace(a)); //NOITAMROFNI } public static String StringReverseInPlace(String toReverse) { char[] chars = toReverse.toCharArray(); int inputStringLength = toReverse.length(); for (int i = 0; i < inputStringLength / 2; i++) { int toMoveBack = toReverse.charAt(i); int toMoveForward = toReverse.charAt(inputStringLength - i - 1); //swap toMoveForward = toMoveBack - toMoveForward; toMoveBack -= toMoveForward; toMoveForward += toMoveBack; chars[i] = (char) toMoveBack; chars[inputStringLength - i - 1] = (char) toMoveForward; return String.valueOf(chars); 4/26/2018
30
Null Initialization vs Empty String
4/26/2018
31
Frequently Used String Methods
4/26/2018
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.