3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Packages, Characters, Strings Arguments to method main CS2110, Week 2 Recitation 1.
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 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.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Introduction to Computers and Programming Lecture 7:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
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 ,
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
String Escape Sequences
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.
BPJ444: Business Programming Using Java – String Handling Tim McKenna
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Characters, Strings, Reading Files CS2110, Week 2 Recitation.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Agenda ASCII char N int Character class char N String Bitwise operators homework.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
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:
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
17-Feb-16 String and StringBuilder Part I: String.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Variables and Types
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Chapter 6 GC 101 Strings.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Primitive Types Vs. Reference Types, Strings, Enumerations
String and StringBuilder
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
String and StringBuilder
String and StringBuilder
String and StringBuilder
String methods 26-Apr-19.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Week 3 - Friday COMP 1600.
Presentation transcript:

3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230 Lecture 03 Monday, February 5 Handout #10 (Revised Friday, February 9)

3 - 2 Characters Characters are the fundamental unit of strings & files Character literals: letters: ‘a’, ‘b’, …, ‘z’, ‘A’, ‘B’, … ‘Z’ digits: ‘0’, ’1’, …, ‘9’ punctuation: ‘,’, ‘.’, ‘;’, ‘!’, ‘ ’ (space), … escaped characters: –‘\’’ (single quote) –‘\”’ (double quote) –‘\\’ (backslash) –‘\n’ (newline) –‘\r’ (carriage return) –‘\t’ (tab) –‘\b’ (backspace) –‘\f’ (form feed)

3 - 3 ASCII Character Representations ASCII = American Standard Code for Information Exchange Defines integer representations for 2^8 = 256 characters (see lowercase letters: ‘a’ ↔ 97, ‘b’ ↔ 98, …, ‘z’ ↔ 122 uppercase letters: ‘A’ ↔ 65, ‘B’ ↔ 66, …, ‘Z’ ↔ 90 digits: ‘0’ ↔ 48, ‘1’ ↔ 49, …, ‘9’ ↔ 57 punctuation: ‘ ’ ↔ 32, ‘!’ ↔ 33, ‘(‘ ↔ 41, ‘)‘ ↔ 42, … escaped chars: ‘\n’ ↔ 10, ‘\r’ ↔ 13, ‘\”’ ↔ 34, ‘\’’ ↔ 39, ‘\\’ ↔ 92 Use casts to convert between characters and integers: (int) ‘A’ → 65, (int) ‘a’ → 97 (char) 65 → ‘A’, (char) 97 → ‘a’ Can compare characters directly via =, >: ‘a’ < ‘b’ → true, ‘b’ == ‘b’ → true, ‘c’ <= ‘0’ → false More modern Unicode representations define integer representations for 2^16 = characters; ASCII are the first 256 of these.

3 - 4 Putting ASCII Representations to Work public static boolean isLetter (char c) { return (‘a’ <= c && c <= ‘z’) || (‘A’ <= c && c <= ‘Z’); } public static boolean isDigit (char c) { return (‘0’ <= c && c <= ‘9’); } public static boolean isUpperCase (char c) { // isLowerCase is similar return ‘A’ <= c && c <= ‘Z’; } public static char toLowerCase (char c) { // toUpperCase is similar return isUpperCase(c) ? (char) (((int) c) + 32) : c; } public static String toString (char c) { return “” + c; } public static char shiftChar (char c, int shift) { return (char) MathOps.mod(((int) c) + shift, 256); } // MathOps.mod(x,m) returns x modulo m

3 - 5 Class Methods of the Character Class Many character methods are already provided as class methods of the Character class, so we needn’t define them from scratch. Morever, these work on full unicode characters, not just ASCII subset. E.g.: public static boolean isLetter (char c); public static boolean isDigit (char c); public static boolean isLetterOrDigit (char c); public static boolean isWhitespace (char c); public static boolean isUpperCase (char c); public static boolean isLowerCase (char c); public static char toLowerCase (char c); public static char toUpperCase (char c); public static String toString (char c); See Java API of the Character class for the complete list of methods.Java API of the Character class

3 - 6 Java Strings vs. Character Arrays char [] chars = {‘c’, ‘s’, ‘2’, ‘3’, ‘0’}; String str = “cs230”; Like arrays of characters, Java strings are sequences of characters indexed from 0: str.charAt(0) → ‘c’, …, str.charAt(4) → ‘0’ chars[0] → ‘c’, …, chars[4] → ‘0’ However, Java Strings differ from character arrays in important ways: Strings are instances of a class (String), but arrays are not instances of any class; can invoke String instance methods on strings. Strings are immutable --- cannot be changed once created. Different notations for String literals (double quotes) and accessing elements at an index (charAt() method).

3 - 7 Some String Instance Methods String s1 = “delivery”; s1.length() → 8 s1.charAt(3) → ‘i’ s1.charAt(8) → StringIndexOutOfBoundsException s1.indexOf(‘e’) → 1 s1.indexOf(‘q’) → -1 // Indicates character is not present s1.indexOf(‘e’, 2) → 5 s1.indexOf(‘e’, 9) → -1 // *No* exception here! s1.indexOf(“live”) → 2 s1.indexOf(“love”) → -1 s1.indexOf(“live”, 9) → -1 // *No* exception here!

3 - 8 More String Instance Methods s1.substring(4) → “very” s1.substring(8) → “” s1.substring(9) → StringIndexOutOfBoundsException s1.substring(2, 6) → “live” s1.substring(4,8) → “very” s1.substring(5,5) → “” s1.substring(6,9) → StringIndexOutOfBoundsException s1.substring(7,6) → StringIndexOutOfBoundsException s1.toUpperCase() → “DELIVERY” (s1.toUpperCase()).toLowerCase() → “delivery” s1.toCharArray() → {‘d’, ‘e’, ‘l’, ‘i’, ‘v’, ‘e’, ‘r’, ‘y}

3 - 9 Even More String Instance Methods String s2 = "I say \t\"Yes\"\nyou say \t\"No\"\nI say \t\"Why?\""; System.out.println(s2); I say "Yes" you say "No" I say "Why?" s2.length() → 40 s2.charAt(6) → ‘\t’ s2.charAt(7) → ‘\”’ s2.charAt(12) → ‘\n’ s2.replace('s', 'S') → "I Say \t\"YeS\"\nyou Say \t\"No\"\nI Say \t\"Why?\"" s2.replaceAll("say", "said") → "I said \t\"Yes\"\nyou said \t\"No\"\nI said \t\"Why?\"" s2.replaceAll("[\t\n]", " ") // [\t\n] is a regular expression denoting a tab *or* a newline. → "I say \"Yes\“ you say \"No\“ I say \"Why?\"“ "aabaaacadaaaae".replaceAll(“a+”,”XYZ”) → “XYZbXYZcXYZdXYZe” // a+ is a regular expression denoting any nonempty sequence of a’s "p \t \n q \n rs \t t".replaceAll("\\s+", " ") → “p q rs t” // \s+ is a regular expression denoting any nonempty sequence of whitespace // characters (e.g., spaces, tabs, newlines, carriage returns). The extra backslash // is needed to pass the backslash before the s to the regular expression checker.

Yet More String Instance Methods s2.split("\n") → {"I say \t\"Yes\"", "you say \t\"No\"", "I say \t\"Why?\""} s2.split("[\n\t]"); → {"I say ", "\"Yes\"", "you say ", "\"No\"", "I say ", "\"Why?\""} s2.split("[\n\t ]"); // [\n\t ] is a regular expression denoting a // newline *or* a tab *or* a space. → {"I", "say", "", "\"Yes\"", "you", "say", "", "\"No\"", "I ", "say", "", "\"Why?\""} // The empty strings come from three space/tab combinations in the input. s2.split("\\s+"); // see notes on \s+ on the previous slide → {"I", "say", "\"Yes\"", "you", "say", "\"No\"", "I ", "say", "\"Why?\""} // Using \s+ removes the empty strings "\t \na\t \nb\t \n".trim() → "a\t \nb" How do we convert strings like "{ 17, -3, 42 }" to the corresponding integer arrays (e.g. {17,-3,42})? (See IntArray.fromString() in utils directory.) See Java API of the String class for the complete list of String methods.Java API of the String class

String Comparisons "cs230".equals("cs230") → true "cs230".equals("CS230") → false "cs230" == "cs230" → Could be true, could be false! "prefer".compareTo("radar") → a negative integer "prefer".compareTo("preference") → a negative integer "prefer".compareTo("prefer") → 0 "prefer".compareTo("like") → a postive integer

Simple String Methods // Assume these are define in a StringOps class // first("cs230") → ‘c’ public static char first (String s) { } // butFirst("cs230") → " s230 " public static String butFirst (String s) { } // last("cs230") → ‘0’ public static char last (String s) { } // butLast("cs230") → "cs23" public static String butLast (String s) { }

String Mapping 1 (Use Iteration) public static String toUpperCase (String s) { } How would we modify the above to implement the following? public static String replace (String s, char oldChar, char newChar); public static String shiftString (String s, int shift); // StringOps.shiftString("HAL", 1) → "IBM"

String Mapping 2 (Use Recursion) public static String toUpperCase (String s) { }

String Filtering (Iterative and Recursive) // StringOps.removeChar(“delivery”, ‘e’) → “dlivry” // StringOps.removeChar(“delivery”, ‘d’) → “elivery” // StringOps.removeChar(“delivery”, ‘q’) → “delivery” public static String removeChar (String s, char c) { }

String Reversal (Iterative and Recursive) // StringOps.reveree(“delivery”) → “yreviled” public static String reverse (String s) { }