Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Recitation 2 Main Method, API & Packages, Java Basics.
Characters, Strings and the String Buffer Jim Burns.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
CS1101: Programming Methodology Aaron Tan.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
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.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
String and String Buffers
Strings Chapter 6.
OUTPUT STATEMENTS GC 201.
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
String and StringBuilder
String and StringBuilder
CS2011 Introduction to Programming I Strings
Outline Creating Objects The String Class The Random and Math Classes
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Presentation transcript:

Java Methods A & AB Chapter 10 - Strings

Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to format Strings Use the Character class to identify parts of Strings

Ch 10 Literal Strings – written in double quotes Ex) – String name = “Steven Austin”; Escape characters are interpreted ex) – String phrase = “Steve\n Austin”; The output is on two lines How do you output – “c:\AP\ch10.txt”?

Ch 10 Strings are immutable This means the value of a string can never change However a string object can reference a new value When this happens, java conveniently deletes the old value Ex) String name = “Bob”; String name = “Bobby”; //exists in different place in memory String name = “Bobby”; //exists in different place in memory

ch10 The String class has 9 constructors This means you can send it 9 different combinations of parameters String str = new String(); String str = new String( array, 4, 10); Above will print 10 characters from the array, starting with the 5 th value

Ch 10 We will typically just declare and initialize a String as follows: String str = “”; //empty String There are several methods in String class String name = “Mickey”; name.charAt(1); //returns char i name.concat(“ Mouse”); //returns Mickey Mouse name.startsWith(“Mick”); //boolean returns true Check out java doc for other methods What does name.length(); return?

Ch 10 Convert a number into a string: Just concatenate a string int n = 45; String s = “” + n; // now n=“45” You can also use a String method String s = Integer.toString(n); This is called Wrapping – converting a primitive data type into an object

Ch 10 Use Decimal Format class to convert doubles into Strings with specific formts Ex) DecimalFormat hundreths = new DecimalFormat(“0.00”); Double num = 35.2 String s1 = hundreths.format(num); s1 gives “35.20”

Ch 10 Printf Outputs a formatted String %f – double – can do field width and decimal places %d – int %c – char, %s - String Double pct = 55.55; Int amt = 103; String title = “Percent and Amount”; What does the following print? System.out.print(“%5.3f,%2d,%c”, pct,amt,title);

Ch 10 Extracting numbers from Strings String s = “43”; int n = Integer.parseInt(s); //uses Integer class This sometimes can cause an error, if the String isn’t a properly formatted number See page 275 to deal with this situation

Ch 10 Character methods Has convenient methods boolean methods isDigit, isLetter, isUpperCase, isLowerCase, isWhiteSpace, etc.. Can also convert Ex) toUpperCase method

Ch 10 StringBuffer class – character strings that can be modified Constructors StringBuffer() – empty string with capacity of 16 char StringBuffer(int n) – empty string with capacity of n char StringBuffer(String s) – constructs a string that holds a copy of s Check page 279 for some cool methods

Ch 10 Common methods length() – returns int of size of string capacity() – returns how big the buffer is

ch10 Example) StringBuffer sb = new StringBuffer(9); sb.append(“we”);Sb.insert(0,’a’);Sb.insert(3,’ss’);sb.setCharAt(4,’o’);Sb.append(“met”);Sb.deleteCharAt(8); String str = sb.toString(); What does str hold?

Ch 10 Exercise set #1,2,3,4a,5a,6,9,18,19,22 Show runs, no printing