Chapter 7: The String class We’ll go through some of this quickly!

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

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
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.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
String Tokenization What is String Tokenization?
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,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Regular Expressions.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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)
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Text Processing and More about Wrapper Classes Starting.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
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.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
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.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Introduction to programming in java
String and StringBuffer classes
Strings.
Programming in Java Text Books :
String Handling in JAVA
MSIS 655 Advanced Business Applications Programming
An Introduction to Java – Part I, language basics
Lecture 07 String Jaeki Song.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Dr. Sampath Jayarathna Cal Poly Pomona
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:

Chapter 7: The String class We’ll go through some of this quickly!

Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!"); Strings are so common, Java provides a handier way of creating them: String s = "Hi mom!"; Strings have methods: loads of them!!!

Some methods for Strings String s1 = "harry"; String s2 = "harold"; if ( s1.equals(s2) ) System.out.println("Strings are the same"); if ( s1.compareTo(s2) < 0 ) System.out.println(s1 + "...." + s2); else System.out.println(s2 + "...." + s1);

Comparing Strings s1.compareTo("hi mom!") returns a value < 0 if this s1 is ordered before "hi mom" returns a value > 0 if "hi mom" is ordered before s1 returns 0 if they are the same Ordering is called lexicographic order

Quick exercise Write a program which reads 2 strings and writes them out in lexicographic order, smallest first Can you figure out which characters are ordered before which? Is this the same as the telephone book?

String concatenation String s1 = "Hi" + " mom"; String s2 = "your lucky number is " + number; String s3 = s1.concat(s2); String s3 = s1 + s2; A number will be first turned into a String, then concatenated.

Substrings A String is stored as an array of characters: Hi_Mom! index: character: public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)

Some things to remember The index of the first character is 0, not 1 substring(a, b) returns the characters from position a to position b-1, not b!!!! substring(a) returns the characters from position a to the end of the String

Other String methods return the character at a given index: –public char charAt(int index) get the length of a String: –public int length()

Processing string contents: StringTokeniser All the strings we’ve seen before have been short (a word or two). To process long strings (such as sentences) we need to be able to split up strings into their parts (words, numbers, etc.). The parts of a sentence are called tokens. “This is a string with 9 tokens in it.” token How do we recognise tokens? They are separated by delimiters (in the sentence above, blank spaces).

Java.util.StringTokenizer // this program uses a StringTokenizer object to split a sentence // into words and print each word on a different line import java.util.StringTokenizer; public class TestTokenizer{ public static void main(String[] args){ String test = “This is a test string.”; StringTokenizer testTokenizer = new StringTokenizer(test); // ‘testTokenizer’ is an object that will give // us the consecutive tokens in the String ‘test’. while (testTokenizer.hasMoreTokens()) { System.out.println(testTokenizer.nextToken()); } // the testTokeniser object has methods called // hasMoreTokens() and nextToken(), which tell // us whether there are more tokens left in the string // test, and give us the next token from that string } }

using StringTokenizer To tokenize a String (e.g. split it into words), we create a new StringTokenizer object, giving the StringTokenizer constructor the string we want to split up: String test = “This is a test string.”; StringTokenizer testTokenizer = new StringTokenizer(test); The StringTokenizer object now has inside it the string to tokenize. We get the next token by asking that object for nextToken(). The object looks in the string it was given at construction, and returns the next token to us. We can find out how many tokens are in our string in total by asking that object for countTokens(). The object looks in the string it was given at construction, and tells us how many tokens are in it.

Delimiters for StringTokenizer By default, a StringTokenizer object splits up a String using blank spaces, tabs ( ‘\t’ ), new line ( ‘\n’ ), return ( ‘r’ ) as delimiters. We can use different delimiters, by giving a String containing the delimiters we want to use as arguments to nextToken(): nextToken(“,\t\n\r”); Uses spaces, commas, tabs, newlines returns as delimiters. We can also specify the delimiters we want to use when we construct our StringTokenizer object: StringTokenizer st=new StringTokenizer(test,“,\t\n\r”); If we want the delimiters to be returned as tokens, we specify that in the constructor as well (normally they’re not returned).

UML for StringTokenizer UML means Unified Modelling Language; it’s a way of summarising object oriented programs quickly. Read the first bit of Liang, Appendix G (p. 903), which explains UML. Here’s the UML for StringTokenizer: StringTokenizer +countTokens(): int +hasMoreTokens(): boolean +nextToken(): String +nextToken(delim: String): String The + means “publically accessible method”. : int means this method returns an integer.