Docsity

Text Processing in Java: Characters and Strings - Reading and Manipulation, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
1
3 - 1
Text Processing In Java:
Characters and Strings
Reading: Downey: Chapter 7
Problem Set: Assignment #1 due Tuesday, Febu rary 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)
pf3
pf4
pf5
pf8

Partial preview of the text

Download Text Processing in Java: Characters and Strings - Reading and Manipulation and more Assignments Data Structures and Algorithms in PDF only on Docsity!

3 - 1

Text Processing In Java:

Characters and Strings

Reading: Downey: Chapter 7 Problem Set: Assignment #1 due Tuesday, Feburary 13 Wellesley College CS Lecture 03 Monday, February 5 Handout # (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 http://www.asciitable.com):

  • 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 = 65536 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 - 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โ€) โ†’ - 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. 3 - 10

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.

3 - 13

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" 3 - 14

String Mapping 2 (Use Recursion)

public static String toUpperCase (String s) { }

3 - 15

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) { } 3 - 16

String Reversal (Iterative and Recursive)

// StringOps.reveree(โ€œdeliveryโ€) โ†’ โ€œyreviledโ€ public static String reverse (String s) { }