/
Lecture 10 Strings CSE 1322 4/26/2018 10- 1 String class A string is a Lecture 10 Strings CSE 1322 4/26/2018 10- 1 String class A string is a

Lecture 10 Strings CSE 1322 4/26/2018 10- 1 String class A string is a - PowerPoint Presentation

liane-varnes
liane-varnes . @liane-varnes
Follow
346 views
Uploaded On 2019-11-01

Lecture 10 Strings CSE 1322 4/26/2018 10- 1 String class A string is a - PPT Presentation

Lecture 10 Strings CSE 1322 4262018 10 1 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 ID: 761746

int string mystring 2018 string int 2018 mystring length index substring river world start indexof public 201810 greeting strings

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture 10 Strings CSE 1322 4/26/2018 10..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

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

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 instantiatedAfter 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/201810-2

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 10- 3

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 10- 4

String Object vs String Reference 4/26/2018 10-5String Constant Pool

String Object vs String Reference 4/26/2018 10-6String Constant Pool

Concatenation Use the + operator: If one of the arguments of the + operator is a string, the other is converted to a stringString name = "Dave";String message = "Hello, " + name; // message is "Hello, Dave"String a = "Agent";int n = 7;String bond = a + n; // bond is Agent74/26/2018 10- 7

Concatenation Use the + operator: If one of the arguments of the + operator is a string, the other is converted to a stringstring 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 10- 8

Concatenation in Print Statements Useful to reduce the number of System.out.print instructions versusSystem.out.print("The total is ");System.out.println(total);System.out.println("The total is " + total);4/26/201810-9

Concatenation in Print Statements Useful to reduce the number of Console.Write instructions versusConsole.Write("The total is ");Console.WriteLIne(total);Console.WriteLine("The total is " + total);4/26/201810- 10

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/201810-11

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 10- 12

Substrings Java Supply start and “past the end” position First position is at 0String greeting = "Hello, World!";String sub = greeting.substring(0, 5); // sub is "Hello" 4/26/201810-13

Substrings Java Substring length is “past the end” - start 4/26/2018 String greeting = "Hello, World!"; String sub = greeting.substring (7, 12); // sub is “World" 10- 14

Substrings C# Supply start and the number of characters desiredFirst position is at 0string greeting = "Hello, World!";string sub = greeting.substring(0, 5); // sub is "Hello" 4/26/201810- 15

Substrings Java 4/26/2018 String greeting = "Hello, World!";String sub = greeting.substring(7, 12); // sub is “World" 10-16

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 10- 17

Answers Agent5ississississippi Agent5ississipississippi4/26/201810-18

4/26/2018 10-19

Length Property public int Length { get; } The number of characters in the current string.RemarksThe 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- 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 10- 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 10- 22

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 10- 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 10- 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 10- 25

Problem-1: Reverse a String (Using a Char Array) 4/26/201810- 26

Problem-1: Reverse a String (in place) 4/26/201810- 27

Problem-1: Reverse a String (in place) 4/26/2018 10-28public 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 ; } }

Problem-1: Reverse a String (in place) 4/26/2018 10-29public 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); } }

Null Initialization vs Empty String 4/26/2018 10-30

Frequently Used String Methods 4/26/2018 10-31https://www.tutorialspoint.com/java/java_strings.htm