Lecture 10 Strings CSE 1322 4/26/2018.

Slides:



Advertisements
Similar presentations
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Advertisements

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Fundamental Programming Structures in Java: Strings.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
28-Jun-15 String and StringBuilder Part I: String.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Chapter 7: Characters, Strings, and the StringBuilder.
String and Scanner CS 21a: Introduction to Computing I First Semester,
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
(Dreaded) Quiz 2 Next Monday.
String class.
Programming in Java Text Books :
String Handling in JAVA
Lecture 4 Using Classes Richard Gesick.
String and StringBuilder
MSIS 655 Advanced Business Applications Programming
Chapter 4 – Fundamental Data Types
String and StringBuilder
Chapter 9 Strings and Text I/O
String and StringBuilder
16 Strings.
String and StringBuilder
String methods 26-Apr-19.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
String Class.
What is a String? String s = "compsci"; s c o m p s i
More on iterations using
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

Lecture 10 Strings CSE 1322 4/26/2018

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

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

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

String Object vs String Reference String Constant Pool 4/26/2018

String Object vs String Reference String Constant Pool 4/26/2018

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

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

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

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

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

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

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

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

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

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

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

Answers Agent5 i ssiss ississippi Agent5 i ssissip ississippi 4/26/2018

4/26/2018

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

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

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

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

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

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

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

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

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

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

Null Initialization vs Empty String 4/26/2018

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