©2004 Brooks/Cole Chapter 7 Strings and Characters.

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
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.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
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.
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.
Java Strings in 10 minutes
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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 ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
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.
Characters, Strings and the String Buffer Jim Burns.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
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.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
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.
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.
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. For example,
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String Class.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
String and StringBuilder
Chapter 9 Strings and Text I/O
String and StringBuilder
String and StringBuilder
CS2011 Introduction to Programming I Strings
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Presentation transcript:

©2004 Brooks/Cole Chapter 7 Strings and Characters

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A String is a Sequence of Characters Literal strings are enclosed in double quotes –"hello" Strings are objects that belong to the String class Each character in the string has an index –first character has index 0

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Strings are Objects Variables which refer to Strings are reference variables –Variable memory stores the address of the String object –Data (character sequence) is stored in a different part of memory

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating Strings Declaring a String Variable String message; A literal string is created automatically. –Assignment stores the address in a variable. message = "hello"; A String object can be created with new This is known as instantiation message = new String("hello"); The String class has several constructors (overloaded)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Creation String string1 = new String("Hello"); String string2 = new String("Hello there");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Concatenation We can join two strings into a larger string using the + operator String s1 = "Hot "; String s2 = "Dog"; String s3 = s1 + s2;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Concatenation creates a new String String message = "Start"; message = message + "le";

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Strings are immutable Once created, a String object cannot be modified –String is created with a certain amount of memory All the String methods that appear to modify a String actually return a new String There is a StringBuffer class that is mutable

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String I/O We have used print and println to output Strings to the console We use the next and nextLine methods of the Scanner class to read String data from the keyboard JOptionPane.showInputDialog also reads String data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Methods length() returns the number of characters in the string concat() can be used (like +) to concatenate strings equals() can be used to check if two strings contain the same sequence of characters compareTo() indicates whether one string comes before another

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing Strings for equality The == operator checks whether the same value is stored in two different variables. For reference variables, what is compared is two addresses To compare whether two strings have the same sequence of characters stored in them, use the equals method.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing with == s1==s2 is true s1==s3 is false s1==s4 is true s2==s3 is false s2==s4 is true s2==s4 is false String s1, s2, s3, s4; s1 = "java"; s2 = "java"; s3 = new String("java"); s4 = s1;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Different Reference Variables Can Reference Equal Strings Comparisons s1==s2 is false s1.equals(s2) is true String s1 = new String( "Help"); String s2 = new String( "Help");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comparing Strings with compareTo compareTo does a character by character comparison –uses unicode values to determine order –case-sensitive s1.CompareTo(s2) is –negative if s1 comes before s2 –0 if s1 and s2 have the same sequence of characters –positive if s1 comes after s2 Examples "Hello" comes after "Goodbye" "hello" comes after "Hello" "SMITH" comes after "JONES" "123" comes after "1227" "Beehive" comes before "Behop" "123" comes before "abc"

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More String methods indexOf( char ch), indexOf( String str) return the position of a character or sequence of characters in the String –Returns -1 if absent –Overloaded versions of indexOf and lastIndexOf toUpperCase(), toLowerCase() return strings with all letters of the same case

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Substrings substring( int start, int next) returns a String containing the characters with indexes start through next-1 in the original String –"appaloosa".substring( 2, 5) ---> "pal" –"Helpless".substring( 0, 4) ---> "Help" –"Helpless".substring( 4) ---> "less"

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Character Class Like the other primitive types, there is a wrapper class, Character, for the char type –Can be used to create objects with a single char value for data –Has class methods useful for handling character data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Character Methods Boolean methods that test whether a character belongs to a particular group of characters –isDigit, isLetter, isWhitespace, isUpperCase, isLowerCase Methods to convert characters from one case to another –toUpperCase, toLowerCase

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Converting Primitive Values to Strings String class has valueOf method which is overloaded to work for any primitive type – String valueOf( boolean b) – String valueOf( int I) – String valueOf( double d) – String valueOf( char c) Wrapper classes have a static toString method – String toString( boolean b) in the Boolean class for example

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Converting Strings to Primitive Values Wrapper class have methods to convert a String to a primitive value –Integer.parseInt( String intString) –Integer.valueOf( String intString) If the string is not appropriate for the primitive type, an error will occur

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 StringBuffer Class A StringBuffer is very similar to a String except that a StringBuffer object can be modified This means you can add, insert or replace characters within the StringBuffer rather than having to create a new object for each change. A StringBuffer is created with a capacity which is the number of characters it can hold. –The capacity can change if necessary

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating a StringBuffer StringBuffer() creates a StringBuffer with a capacity of 16 StringBuffer(int length) creates a StringBuffer with a capacity of length StringBuffer( String str) creates a StringBuffer with a capacity of str.length() + 16

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Initial Storage of a StringBuffer Object StringBuffer str = new StringBuffer( "This cannot be");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Modifying the StringBuffer str.insert( 4, " I know"); str.replace( 12, 18, "to");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The StringBuffer After the Append str.append( "correct");