©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 9 Characters and Strings.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
Java Programming Strings Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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 ,
Intro to OOP with Java, C. Thomas Wu Characters and Strings
Fundamental Programming Structures in Java: Strings.
Data types and variables
28-Jun-15 String and StringBuilder Part I: String.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
© 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 ,
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Characters and Strings. Representation of single characters Data type char is the data type that represents single characters, such as letters, numerals,
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Objectives You should be able to describe: Data Types
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (adapted from.
Regular Expressions.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Introduction to Java Java Translation Program Structure
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
By Mr. Muhammad Pervez Akhtar
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Strings, Characters and Regular Expressions
Chapter 3 : Basic Concept Of Java
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
String and StringBuilder
Chapter 7: Strings and Characters
String and StringBuilder
Chapter 2: Basic Elements of Java
String and StringBuilder
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Objectives After you have read and studied this chapter, you should be able to Declare and manipulate data of the char data type. Write string processing programs, using String and StringBuffer objects. Differentiate the String and StringBuffer classes and use the correct class in solving a given task. Tell the difference between equality and equivalence testings for String objects.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.1 Characters In Java, single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes: char ch1 = ‘X’;

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.1 Characters ASCII stands for American Standard Code for Information Interchange. ASCII is one of the document coding schemes widely used today. This coding scheme allows different computers to share information easily.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.1 Characters ASCII works well for English-language documents because all characters and punctuation marks are included in the ASCII codes. ASCII does not represent the full character sets of other languages.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.1 Characters The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages. Java uses the Unicode standard for representing char constants.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.2 Strings A string is a sequence of characters that is treated as a single value. Instances of the String class are used to represent strings in Java. We access individual characters of a string by calling the charAt method of the String object.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.2 Strings Each character in a string has an index we use to access the character. Java uses zero-based indexing; the first character’s index is 0, the second is 1, and so on. To refer to the first character of the word name, we say name.charAt(0).

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 9.1 An indexed expression is used to refer to individual characters in a string.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.2 Strings Since String is a class, we can create an instance of a class by using the new method. The statements we have used so far, such as String name1 = “Kona”; works as a shorthand for String name1 = new String(“Kona”); But this shorthand works for the String class only.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.2 Strings String comparison may be done in several ways. The methods equals and equalsIgnoreCase compare string values; one is case-sensitive and one is not. The method compareTo returns a value: Zero (0) if the strings are equal. A negative integer if the first string is less than the second. A positive integer if the first string is greater than the second.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression Pattern matching is a common function in many applications. In Java 2 SDK 1.4, two new classes, Pattern and Matcher, are added. The String class also includes several new methods that support pattern matching.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The matches method from the String class is similar to the equals method. However, unlike equals, the argument to matches can be a pattern.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression Suppose that all new students are assigned a three-digit code: The first digit represents the major (5 indicates computer science); The second digit represents either in-state (1), out-of-state (2), or foreign (3); The third digit indicates campus housing: On-campus dorms are numbered 1-7. Students living off-campus are represented by the digit 8.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The valid code pattern for computer science majors living on-campus:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The pattern is called a regular expression that allows us to denote a large set of “words” (any sequence of symbols) succinctly. Brackets [] represent choices, so [abc] means a, b, or c. For example, the definition for a valid Java identifier may be stated as [a-zA-Z][a-zA-Z0-9_$]*

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression ExpressionDescription [013] A single digit 0, 1, or 3. [0-9][0-9] Any two-digit number from 00 to 99. A[0-4]b[05] A string that consists of four characters. The first character is A. The second character is a number between 0 and 4, inclusive. The third character is b. The last character is either 0 or 5. [0-9&&[^4567]] A single digit that is 0, 1, 2, 3, 8, or 9. [a-z0-9] A single character that is either a lowercase letter or a digit.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression ExpressionDescription X{N}Repeat X exactly N times, where X is a regular expression for a single character. X{N,}Repeat X at least N times. X{N,M}Repeat X at least N but no more than M times.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The period symbol (.) is used to match any character except a line terminator (\n or \r). String document; document =...; //assign text to ‘document’ if (document.matches(“.*zen of objects.*”){ System.out.println(“Found”); }else{ System.out.println(“Not found”); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression Brackets ([ ]) are used for expressing a range of choices for a given character. To express a range of choices for multiple characters, use parentheses and the vertical bar.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression ExpressionDescription [wb](ad|eed) Matches wad, weed, bad, and beed. (pro|anti)-OOP Matches pro-OOP and anti-OOP (AZ|CA|CO)[0-9]{4} Matches AZxxxx, CAxxxx, and COxxxx, where x is a single digit.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The replaceAll method is new to the Version 1.4 String class. This method allows us to replace all occurrences of a substring that matches a given regular expression with a given replacement string.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression For example, to replace all vowels in a string with symbol: String originalText, modifiedText; originalText =...; //assign string to ‘originalText’ modifiedText = Note that this method does not change the original text; it simply returns a modified text as a separate string.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression To match a whole word, use the \b symbol to designate the word boundary. str.replaceAll(\\btemp\\b, “temporary”); Two backslashes are necessary because we must write the expression in a String representation. Two backslashes prevents the system from interpreting the regular expression backslash as a control character.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.3 Pattern Matching and Regular Expression The backslash is also used to search for a command character. For example: To search for the plus symbol (+) in text, we use the backslash as \+. To express it as a string, we write “\\+”.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes The matches and replaceAll methods of the String class are shorthand for using the Pattern and Matcher classes from the java.util.regex package.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes If str and regex are String objects, then both str.matches(regex); and Pattern.matches(regex, str); are equivalent to Pattern pattern = Pattern.compile(regex); Matcher matcher = p.matcher(str); matcher.matches();

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes Creating Pattern and Matcher objects gives us more options and efficiency. The compile method of the Pattern class converts the stated regular expression to an internal format to carry out the pattern- matching operation. This conversion is carried out every time the matches method of the String or Pattern class is executed.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes /* Chapter 9 Sample Program: Checks whether the input string is a valid identifier. This version uses the Matcher and Pattern classes. File: Ch9MatchJavaIdentifier2.java */ import javax.swing.*; import java.util.regex.*; class Ch9MatchJavaIdentifier2 { private static final String STOP = STOP"; private static final String VALID ="Valid Java identifier"; private static final String INVALID ="Not a valid Java identifier";

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes private static final String VALID_IDENTIFIER_PATTERN = "[a-zA-Z][a-zA-Z0-9_$]*"; public static void main (String[] args) { String str, reply; Matcher matcher; Pattern pattern =Pattern.compile(VALID_IDENTIFIER_PATTERN); while (true) { str = JOptionPane.showInputDialog(null, "Identifier:"); if (str.equals(STOP)) break;

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes matcher = pattern.matcher(str); if (matcher.matches()) { reply = VALID; } else { reply = INVALID; } JOptionPane.showMessageDialog(null, str + ":\n" + reply); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes The find method is another powerful method of the Matcher class. The method searches for the next sequence in a string that matches the pattern, and returns true if the pattern is found.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes When a matcher finds a matching sequence of characters, we can query the location of the sequence by using the start and end methods.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.4 The Pattern and Matcher Classes The start method returns the position in the string where the first character of the pattern is found. The end method returns the value 1 more than the position in the string where the last character of the pattern is found.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.5 Comparing Strings Comparing String objects is similar to comparing other objects. The equality test (==) is true if the contents of the variables are the same. For a reference data type, the equality test is true if both variables refer to the same object, because they both contain the same address.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.5 Comparing Strings The equals method is true if the String objects to which the two variables refer contain the same string value.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 9.2A The difference between the equality test and the equals method.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 9.2B and C

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.5 Comparing Strings As long as a new String object is created using the new operator, the rule for comparing objects applies to comparing strings. String str = new String (“Java”); If the new operator is not used, string data are treated as if they are of the primitive data type. String str = “Java”;

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 9.3 The difference between using and not using the new operator for String.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer When a String object is created, it cannot be changed. Manipulating the content of a string, such as replacing a character, appending a string with another string, deleting a portion of a string, and so on, may be accomplished by using the StringBuffer class.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer For example: StringBuffer word = new StringBuffer(“Java”); word.setCharAt(0, ‘D’); word.setCharAt(1, ‘i’ ); changes the string from “Java” to “Diva.”

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer The following example reads a sentence and replaces all vowels in the sentence with the character X. /* Chapter 9 Sample Program: Replace every vowel in a given sentence with 'X‘using StringBuffer. File: Ch9ReplaceVowelsWithX.java */ import javax.swing.*; class Ch9ReplaceVowelsWithX { public static void main (String[] args) {

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer StringBuffer tempStringBuffer; String inSentence; int numberOfCharacters; char letter; inSentence = JOptionPane.showInputDialog(null, "Enter a sentence:"); tempStringBuffer= new StringBuffer(inSentence); numberOfCharacters = tempStringBuffer.length(); for (int index = 0; index < numberOfCharacters; index++) {

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer letter = tempStringBuffer.charAt(index); if ( letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { tempStringBuffer.setCharAt(index,'X'); } System.out.println( "Input: " + inSentence + "\n"); System.out.println( "Output: " + tempStringBuffer ); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer We cannot input StringBuffer objects. We must input String objects and convert them to StringBuffer objects.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer We use the append method to append a String or StringBuffer object to the end of a StringBuffer object. The method append can also take an argument of the primitive data type. Any primitive data type argument is converted to a string before it is appended to a StringBuffer object.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer We can insert a string at a specified position by using the insert method. The syntax for this method is:.insert(, ); where must be greater than or equal to 0 and less than or equal to the length of, and the is an object or a value of the primitive data type.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 9.6 StringBuffer For example, executing StringBuffer str = new StringBuffer(“Java is great”); str.insert(8, “really”); changes the string Java is great to Java is really great.