Chapter 8—Strings and Characters

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

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.
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.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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 ,
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,
Characters and Strings Eric Roberts CS 106A February 1, 2010.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
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.
©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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Chapter 8—Strings and Characters The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Strings and Characters C H A P T E R 8.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Chapter 8 Characters and Strings. Principle of enumeration Computers tend to be good at working with numeric data. The ability to represent an integer.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
String Handling StringBuffer class character class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC Programming I Lecture 9 September 11, 2002.
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:
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
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.
Programming – Lecture 9 Strings and Characters (Chapter 8) Enumeration ASCII / Unicode, special characters Character class Character arithmetic Strings.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Sorts, CompareTo Method and Strings
Programming – Lecture 9 Strings and Characters (Chapter 8) Enumeration
Chapter 2 Basic Computation
Strings, Characters and Regular Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
University of Central Florida COP 3330 Object Oriented Programming
Programming – Lecture 9 Strings and Characters (Chapter 8) Enumeration
Primitive Types Vs. Reference Types, Strings, Enumerations
Characters Computers use the principle of enumeration to represent character data inside the memory of the machine. There are, after all, a finite number.
String Objects & its Methods
String and StringBuilder
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Introduction to C++ Programming
String and StringBuilder
String and StringBuilder
Chapter 8—Strings and Characters
Binary Representation
slides courtesy of Eric Roberts
String and StringBuilder
String methods 26-Apr-19.
Presentation transcript:

Chapter 8—Strings and Characters The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java C H A P T E R 8 Strings and Characters Surely you don’t think that numbers are as important as words. —King Azaz to the Mathemagician —Norton Juster, The Phantom Toolbooth, 1961 Chapter 8—Strings and Characters

Characters Computers use integers (a code) to represent character data inside the memory of the machine. Character codes, however, are not particularly useful unless they are standardized. If different computer manufacturers use different coding sequence (as was indeed the case in the early years), it is harder to share such data across machines. The first widely adopted character encoding was ASCII (American Standard Code for Information Interchange). With only 256 possible characters, the ASCII system proved inadequate to represent the many alphabets in use throughout the world. It has therefore been superseded by Unicode, which allows for a much larger number of characters.

The ASCII Subset of Unicode The following table shows the first 128 characters in the Unicode character set, which are the same as in the older ASCII scheme: The letter A, for example, has the Unicode value 1018, which is the sum of the row and column labels. The Unicode value for any character in the table is the sum of the octal numbers at the beginning of that row and column. 1 2 3 4 5 6 7 00x \000 \001 \002 \003 \004 \005 \006 \007 \b \t \n \011 \f \r \016 \017 \020 \021 \022 \023 \024 \025 \026 \027 \030 \031 \032 \033 \034 \035 \036 \037 space ! " # $ % & ' ( ) * + , - . / 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ \177 \000 \001 \002 \003 \004 \005 \006 \007 \b \t \n \011 \f \r \016 \017 \020 \021 \022 \023 \024 \025 \026 \027 \030 \031 \032 \033 \034 \035 \036 \037 space ! " # $ % & ' ( ) * + , - . / 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ \177 01x 02x 03x 04x 05x 06x 07x 10x 11x 12x 13x 14x 15x 16x 17x

Notes on Character Representation The important observation is that a character has a numeric representation, and not what that representation happens to be. To specify a character in a Java program, you need to use a character constant, which consists of the desired character enclosed in single quotation marks. Thus, the constant 'A' in a program indicates the Unicode representation for an uppercase A. That it has the value 65 is an irrelevant detail.

Special Characters Most of the characters in the Unicode table are the familiar ones that appear on the keyboard. These characters are called printing characters. The table also includes several special characters that are typically used to control formatting. Special characters are indicated in the Unicode table by an escape sequence, which consists of a backslash followed by a character of sequence of digits. The most common ones are: \b Backspace \f Form feed (starts a new page) \n Newline (moves to the next line) \r Return (moves to the beginning of the current line without advancing) \t Tab (moves horizontally to the next tab stop) \\ The backspace character itself \' The character ' (required only in character constants) \" The character " (required only in string constants) \ddd The character whose Unicode value is the octal number ddd

Special Characters Output: println("Hello\nworld"); println("Hey\tworld"); println("Helloo\tworld"); println("Helloooo \"world\" "); println("Helloooo \\ world"); println("Helloooo \101\102\103 world"); Output: Hello world Hey world Helloo world Helloooo "world" Helloooo \ world Helloooo ABC world

Useful Methods in the Character Class static boolean isDigit(char ch) Determines if the specified character is a digit. static boolean isLetter(char ch) Determines if the specified character is a letter. static boolean isLetterOrDigit(char ch) Determines if the specified character is a letter or a digit. static boolean isLowerCase(char ch) Determines if the specified character is a lowercase letter. static boolean isUpperCase(char ch) Determines if the specified character is an uppercase letter. static boolean isWhitespace(char ch) Determines if the specified character is whitespace (spaces and tabs). static char toLowerCase(char ch) Converts ch to its lowercase equivalent, if any. If not, ch is returned unchanged. static char toUpperCase(char ch) Converts ch to its uppercase equivalent, if any. If not, ch is returned unchanged.

Character Arithmetic The fact that characters have underlying representations as integers allows you can use them in arithmetic expressions. For example, if you evaluate the expression 'A' + 1, Java will convert the character 'A' into the integer 65 and then add 1 to get 66, which is the character code for 'B'. As an example, the following method returns a randomly chosen uppercase letter: public char randomLetter() { return (char) rgen.nextInt('A', 'Z'); } The following code implements the isDigit method from the Character class: public boolean isDigit(char ch) { return (ch >= '0’ && ch <= '9'); }

Exercise: Character Arithmetic Implement a method toHexDigit that takes an integer and returns the corresponding hexadecimal digit as a character. Thus, if the argument is between 0 and 9, the method should return the corresponding character between '0' and '9'. If the argument is between 10 and 15, the method should return the appropriate letter in the range 'A' through 'F'. If the argument is outside this range, the method should return '?'. public char toHexDigit(int n) { if (n >= 0 && n <= 9) { return (char) ('0' + n); } else if (n >= 10 && n <= 15) { return (char) ('A' + n - 10); } else { return '?'; }

Strings as an Abstract Idea Ever since the very first program in the text, which displayed the message "hello, world" on the screen, you have been using strings to communicate with the user. Strings are actually complicated objects whose details are better left hidden. Java supports a high-level view of strings by making String a class whose methods hide the underlying complexity.

What is a string? What is a character? public void run() { println(“Enter a number"); … } What is a character? public void run() { println(“Enter a number"); println(“a”); // not a character println(‘a’); // a character … }

Using Methods in the String Class A char is a primitive type. (like int, boolean, double) A String is an object type; a string is made out of several characters. You may send messages to String objects, just like you send messages to GOval, GLine, etc. objects. You cannot send messages to primitive values. For char values, you have to use the methods of the Character class.

Strings vs. Characters In the Character class, you call toUpperCase as a static method, like this: ch = Character.toUpperCase(ch); In the String class, you apply toUpperCase to an existing string, as follows: str = str.toUpperCase(); Note that both classes require you to assign the result back to the original variable if you want to change its value.

Using Methods in the String Class Java defines many useful methods that operate on the String objects. These methods do not change the value of the String object Instead, they return a new string on which the desired changes have been performed

Selecting Characters from a String Conceptually, a string is an ordered collection (array) of characters. For example, the characters in the string "hello, world" are arranged like this: h e 1 l 2 3 o 4 , 5 6 w 7 8 r 9 10 d 11 You can obtain the number of characters by calling length. You can select an individual character by calling charAt(k), where k is the index of the desired character. The expression returns the first character in str, which is at index position 0. str.charAt(0);

Concatenation One of the most useful operations available for strings is concatenation, which consists of combining two strings end to end with no intervening characters. Concatenation is built into Java in the form of the + operator. If you use + with numeric operands, it signifies addition. If at least one of its operands is a string, Java interprets + as concatenation.

Concatenation public void run() { String s1 = "hello"; String s2 = "world"; int x = 5; double y = 4.5; String s3 = s1 + s2; println(s3); // helloworld println(s1 + s2); // helloworld String s4 = x + s1; println(s4); // 5hello String s5 = s2 + y; println(s5); // world4.5 String s6 = s1 + x + y; println(s6); // hello54.5 String s7 = s1 + (x + y); println(s7); // hello9.5 String s8 = x + y + s1; println(s8); // 9.5hello }

Extracting Substrings The substring method makes it possible to extract a piece of a larger string by providing index numbers that determine the extent of the substring. where p1 is the first index position in the desired substring and p2 is the index position immediately following the last position in the substring. The general form of the substring call is str.substring(p1, p2); As an example, if you wanted to select the substring "ell" from a string variable str containing "hello, world" you would make the following call: str.substring(1, 4);

Checking Strings for Equality Many applications will require you to test whether two strings are equal, in the sense that they contain the same characters. Although it seems natural to do so, you cannot use the == operator for this purpose. While it is legal to write if (s1 == s2) . . . the if test will not have the desired effect. When you use == on two objects, it checks whether the objects are identical, which means that the references point to the same address. What you need to do instead is call the equals method: if (s1.equals(s2)) . . .

String equality Output: public void run() { // Try this program with abcd and abcd String s1 = readLine("Gimme a string bro: "); String s2 = readLine("Yo, gimme another: "); boolean b1 = s1 == s2; boolean b2 = s1.equals(s2); println("s1 == s2 is " + b1); println("s1.equals(s2) is " + b2); } Output: Gimme a string bro: abcd Yo, gimme another: abcd s1 == s2 is false s1.equals(s2) is true

Comparing Characters and Strings The fact that characters are primitive types with a numeric internal form allows you to compare them using the relational operators. If c1 and c2 are characters, the expression c1 < c2 is true if the Unicode value of c1 is less than that of c2. The String class allows you to compare two strings using the internal values of the characters, although you must use the compareTo method instead of the relational operators: s1.compareTo(s2) This call returns an integer that is less than 0 if s1 is less than s2, greater than 0 if s1 is greater than s2, and 0 if the two strings are equal.

Searching in a String Java’s String class includes several methods for searching within a string for a particular character or substring. The method indexOf takes either a string or a character and returns the index within the receiving string at which the first instance of that value begins. If the string or character does not exist at all, indexOf returns -1. For example, if the variable str contains the string "hello, world": str.indexOf('h') returns str.indexOf("o") 4 str.indexOf("ell") 1 str.indexOf('x') -1 The indexOf method takes an optional second argument that indicates the starting position for the search. Thus: str.indexOf("o", 5) returns 8

Other Methods in the String Class int lastIndexOf(char ch) or lastIndexOf(String str) Returns the index of the last match of the argument, or -1 if none exists. boolean equalsIgnoreCase(String str) Returns true if this string and str are the same, ignoring differences in case. boolean startsWith(String str) Returns true if this string starts with str. boolean endsWith(String str) Returns true if this string starts with str. String replace(char c1, char c2) Returns a copy of this string with all instances of c1 replaced by c2. String trim() Returns a copy of this string with leading and trailing whitespace removed. String toLowerCase() Returns a copy of this string with all uppercase characters changed to lowercase. String toUpperCase() Returns a copy of this string with all lowercase characters changed to uppercase

String Creation and Usage public void run() { String s1 = "Java is "; String s2 = "easy"; // int x = 5; String t2 = new String("difficult"); // GOval circle = new GOval(..) String s3 = s1 + t2; println(s3); String up = s3.toUpperCase(); println(up); }

Reading String from user public void run() { String s1 = readLine("Gimme a string bro: "); String s2 = s1.toUpperCase(); println("s1 is " + s1); println("s2 is " + s2); }

Exercise Write a Java program that takes two lines of String input from the user, concatenates them with a space character in between, and prints the resulting string out. Sample output should be as follows. Enter a string: Hello Enter a string: World Concatenated string: Hello World

Simple String Idioms When you work with strings, there are two idiomatic patterns that are particularly important: 1. Iterating through the characters in a string. for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); . . . code to process each character in turn . . . } 2. Growing a new string character by character. String result = ""; for (whatever limits are appropriate to the application) { . . . code to determine the next character to be added . . . result += ch; }

Exercises: String Processing As a client of the String class, how would you implement toUpperCase(str) so it returns an uppercase copy of str? public String toUpperCase(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); result += Character.toUpperCase(ch); } return result; How would you code the method indexOf(ch, str) that returns the index of the first occurrence of ch in str? public int indexOf(char ch, String str) { for (int i = 0; i < str.length(); i++) { if (ch == str.charAt(i)) return i; } return -1;

The reverseString Method public void run() { println("This program reverses a string."); String str = readLine("Enter a string: "); String rev = reverseString(str); println(str + " spelled backwards is " + rev); } str STRESSED rev DESSERTS public void run() { println("This program reverses a string."); String str = readLine("Enter a string: "); String rev = reverseString(str); println(str + " spelled backwards is " + rev); } private String reverseString(String str) { String result = ""; for ( int i = 0; i < str.length(); i++ ) { result = str.charAt(i) + result; } return result; i str result rev str DESSERTS STRESSED RTS ESSERTS TS S SERTS ERTS DESSERTS SSERTS STRESSED 8 7 2 1 6 3 4 5 ReverseString This program reverses a string. Enter a string: STRESSED STRESSED spelled backwards is DESSERTS skip simulation

ReverseString v2 private String reverseString(String str) { String result = ""; for ( int i = str.length()-1; i >= 0; i-- ) { result += str.charAt(i); } return result;

The StringTokenizer Class Can be used for dividing a String into words imported as java.util.StringTokenizer Divides a string into independent units called tokens. These tokens can be read one at a time. StringTokenizer splits a string into a set of tokens that are separated by a special string called the delimiter. By default, space characters are assumed to be the delimeter

The StringTokenizer Class The constructor for the StringTokenizer class takes three arguments, where the last two are optional: A string indicating the source of the tokens. A string which specifies the delimiter characters to use. By default, the delimiter characters are set to the whitespace characters. Once you have created a StringTokenizer, you use it by setting up a loop with the following general form: while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); code to process the token }

Exercise Write a Java program that takes a String input from the user, retrieves tokens splitted with spaces (“ ”) and prints them out in uppercase letters

Tokenizer import acm.program.*; import java.util.StringTokenizer; public class StringLecture extends ConsoleProgram { public void run() { String inputStr = readLine("Enter a string: "); StringTokenizer tokenizer = new StringTokenizer(inputStr, " "); while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); token = token.toUpperCase(); println(token); }

Exercise Write a method called acronym that takes a String as argument and returns another String standing for the acronym of this argument. Write the run method to take a String input from the user and print out the acronym for it (obtained using the acronym method). Sample output should be as follows. Enter a string: Turkish Republic Acronym: TR

Acronym public String acronym(String s){ // Uses StringTokenizer. Imported from java.util package String output = ""; StringTokenizer tokenizer = new StringTokenizer(s); while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); output += token.charAt(0); } return output;

Exercise Write a method called beginsWith that takes two Strings s1 and s2, and determines whether s1 starts with s2. Do NOT use the method startsWith available in the String class. public boolean beginsWith(String s1, String s2) { if (s1.length() < s2.length()) return false; for(int i=0; i < s2.length(); i++) { if(s1.charAt(i) != s2.charAt(i)) } return true;

Exercise Write a method called compare that takes two Strings s1 and s2 and returns 1 if s1 is (lexicographically) less than s2, returns -1 if s1 is (lexicographically) bigger than s2, returns 0 otherwise. Do NOT use the method compareTo available in the String class.

compare public int compare(String s1, String s2) { int length = Math.min(s1.length(), s2.length()); for (int i = 0; i < length; i++) { if (s1.charAt(i) > s2.charAt(i)) return -1; if (s1.charAt(i) < s2.charAt(i)) return 1; } // If we reach this point, we’ve run out of characters // of one of the strings and every char is equal to the // corresponding char in the other string. // The last thing to check is the lengths of the strings. if (s1.length() > s2.length()) else if (s2.length() > s1.length()) else return 0;

Exercise Write a method called toPosition that takes a String and replaces the characters in the string with their position in the word they are located. Enter your line: The force is strong with him Result: 123 12345 12 123456 1234 123

compare public String toPosition(String s) { String result = ""; int position = 1; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { result += s.charAt(i); position = 1; // reset the counter } else { result += position; position++; } return result;