Strings! Today! CSE String Methods!

Similar documents
CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

"Hello" " This " + "is String " + "concatenation"

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

STUDENT LESSON A10 The String Class

CST141 Thinking in Objects Page 1

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

String related classes

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 20/Fri Nov 21.

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 28/Fri Nov 29.

Class. Chapter 6: Data Abstraction. Example. Class

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

String is a Class; Quoted Text is an Object

CS112 Lecture: Characters and Strings

Lab 14 & 15: String Handling

More non-primitive types Lesson 06

More on variables and methods

H212 Introduction to Software Systems Honors

Lecture 4. Strings, Wrappers, & Containers

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

- Thus there is a String class (a large class)

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

STRINGS AND STRINGBUILDERS. Spring 2019

Lesson:9 Working with Array and String

Designing data types. Fundamentals of Computer Science I

A variable is a name for a location in memory A variable must be declared

Lesson 4 Utility classes: Math, String, I/O

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

Programming with Java

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

CS 1301 Ch 8, Part A

We now start exploring some key elements of the Java programming language and ways of performing I/O

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Chapter 10: Text Processing and More about Wrapper Classes

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Using Java Classes Fall 2018 Margaret Reid-Miller

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

USING LIBRARY CLASSES

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

JAVA Programming Fundamentals

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

Welcome to the Using Objects lab!

Activity 9: Object-Oriented

Java By Abstraction - Test-B (Chapters 1-6)

Bellwork Strings 11/25/13

appreciate the difference between a char and a string understand and use the String class methods

Creating Strings. String Length

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CSE Lecture 24 Review and Recap. High-Level Overview of the Course!! L1-7: I. Programming Basics!

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

String. Other languages that implement strings as character arrays

York University Fall 2001 / Test #1 Department of Computer Science

Fall 2017 CISC124 9/27/2017

Lecture 8: The String Class and Boolean Zen

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

c) And last but not least, there are javadoc comments. See Weiss.

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

Eng. Mohammed Abdualal

Contents. Osp::Base::Object Osp::Base::String Osp::Base::Buffer Osp::Base::DateTime

Activity 9: Object-Oriented

Netbeans tutorial:

CSE 143 Au03 Midterm 2 Page 1 of 7

Java Programming Fundamentals. Visit for more.

CHAPTER 6 MOST COMMONLY USED LIBRARIES

CSC 222: Object-Oriented Programming. Spring 2017

Java Fall 2018 Margaret Reid-Miller

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Computer Science 145 Midterm 1 Fall 2016

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

String Objects: The string class library

CS Week 5. Jim Williams, PhD

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Lists, loops and decisions

CSC Java Programming, Fall Java Data Types and Control Constructs

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Fall 2017 CISC124 10/1/2017

CS 106A, Lecture 9 Problem-Solving with Strings

CSC 222: Object-Oriented Programming. Spring 2013

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Lecture 2: Intro to Java

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

CS 106 Introduction to Computer Science I

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Transcription:

Strings CSE 1710 Lecture 14, 15 String Handling We have covered three chunks of material: Week 1: String Literals pp. 22-23; Fig 1.12; PT 1.8 Week 6: The String Class Section 6.1.1, pp. 219-220 The Masquerade and the + Operator Section 6.1.2, pp. 221-224 2 Today The String Class Section 6.2 pp. 219-220 String Methods length() charat(int) substring(int,int) (int) indexof(string) (String,int) tostring() and equals() compareto() touppercase() and tolowercase() 3 Copyright 2006 Pearson Education Canada Inc.

REMEMBER Any string is represented by an object A variable of type String is used to store the address of the object. The String object has a state the state of an object is defined as the value of all its attributes the only attribute of a String object is the attribute that represents the sequence of characters the state of a String object basically boils down to what is its sequence of characters? 5 REMEMBER If the state of a String object is such that its sequence has no characters at all, how do we understand this? this is the empty string the string has length zero THIS IS NOT A NULL STRING What is this null string? technically speaking, null string is not really a correctlyformed term, there is no such thing HOWEVER, it is often used to mean a string reference that is set to null. This means that a String reference has been declared, but that there is NO String object. 6 Can we modify the state of a String object? NO Once a string object is created, it cannot be changed. This is called immutability Strings are immutable This is an unusual property MOST other objects are mutable But what if we need to modify the state of a String object? Instead of modifying the sequence, we just create new strings that are modified verisons of the originals. It is fast and easy, thanks to the + operator Given this, is it correct to say that String has mutators? not technically; they are actually generators of new modified objects 7 8

int : length() method char : charat(int) method str1.length() returns an int tells us the number of characters in the object s character sequence remember the indexing of the character positions starts at 0 str1.charat(idx1) returns a char gives us the character at the specified index remember the first character of a string that is n characters long is at index 0 and the last character is at index n-1 9 10 String : substring(int, int) method String : substring(int) method what do each of these methods do? these methods must return a brand new string substring(idx1,idx2) returns a String gives a subset of the character sequence from the start index inclusive to the end index exclusive Can you live w/o substring(int) given the overloaded (int,int)? 11 int : indexof(char) method int : indexof(char, int) method what do each of these methods do? str1.indexof(str2) returns an int if str2 does not occur within str1, the method gives us the value -1 if str2 does occur within str1, the method gives us a value which is the index at which str2 occurs in str1 s character sequence if str2 occurs more than once within str1, the method gives us a value which is the index at which str2 first occurs in str1 s character sequence str1.indexof(str2, idx1) returns an int just like str1.indexof(str2), but the methods looks at str1 s character sequence only starting at index position idx1 onwards str1.substring(idx1) [REVISITED] just like str1.substring(idx1, idx2), with the assumption that idx2 is the length of str1 12

int : indexof(char) method int : indexof(char, int) method How would use use indexof to detect all occurrences of a substring? str1.substring(idx1) returns a String just like str1.substring(idx1, idx2), with the assumption that idx2 is the length of str1 anything you do using str1.substring(idx1), you could also do with str1.substring(idx1, idx2) CONVINCE YOURSELVES OF THIS String : tostring() method boolean : equals(string) method Do not underestimate what equals does str1.equals(str2) returns a boolean tells us whether str2 has the same state as str1 not whether str2 is the same object as str1 13 14 String matching/comparison (basic) Suppose c1, c2 are chars Suppose s1, s2 are Strings int : compareto(string) method what does the equality boolean operator == tell us? boolean ismatch = c1==c2; boolean ismatch = s1==s2; what does.equals(string) tell us? boolean ismatch = s1.equals(s2); what does.compareto(string) tell us? int differingindexpos = s1.compareto(s2); 15 16

Elaboration of compareto(string) (sort of) tell me whether the passed string comes before this string in the dictionary aardvark.compareto( anvil ) anvil does not come before aardvark in the dictionary, so the result is no (negative value) anvil.compareto( aardvark ) aardvark does come before anvil in the dictionary, so the result is yes (positive value) (better) tell me whether the passed string comes before this string in the dictionary and, for the first character that is the determining factor, what is the distance the second character is the determining factor ( a vs n, there is a distance of 13 between them) 17 str1.compareto(str2) returns an int gives us an int that is a coded message 0 if if str1 and str2 are equal polarity (the sign, +ve or ve) tells us whether str2 comes before str1 in the dictionary. dictionary uses lexicographic ordering if str1 and str2 are not equal, then the value is Unicode difference of the first differing character if there is no index position at which they differ, then the value is the length difference 18 String : touppercase() method String : tolowercase() method these methods must return a brand new string str1.touppercase() returns a String str2.tolowercase() returns a String these are NOT mutators each returns a String obj, which is an entirely new object that is modified version of str1 str1 is not changed at all (in fact, it cannot be changed, since it is immutable) 19 Comparing strings: equals vs matches suppose we have two strings, str1 and str2 str1.equals(str2) returns true iff str1 has the same state as str2 str1.matches(str2) returns true iff str2 matches the pattern as stipulated by str2 FOR NOW, WE WILL DO DEAD SIMPLE PATTERNS 20

hello.matches( hello ) in the context of being a parameter to matches, str2 is interpreted as a regular expression (aka REGEX) the REGEX specifies 5 criteria: REGEX criteria hello.matches( hello ) that the character h is in index position 0 that the character e is in index position 1 that the character l is in index position 2 that the character l is in index position 3 that the character o is in index position 4 (implied) no further characters in the sequence Criterion satisfied? yes yes yes yes yes yes Regular expressions: Simple classes a regular expression can also use special characters and syntax to specify more patterns more generally [abc] defines a simple class of characters REGEX criteria hello.matches( [Hh]ello ) the character H or h is in index position 0 the character e is in index position 1 the character l is in index position 2 the character l is in index position 3 the character o is in index position 4 no further characters in the sequence str1 satisfies? yes yes yes yes yes yes L17App2 21 22 Regular expressions: Simple classes using a range [a-d] defines a simple class using a range REGEX criteria hello.matches( [a-d]ello ) the character a or b or c or d is in index position 0 the character e is in index position 1 the character l is in index position 2 the character l is in index position 3 the character o is in index position 4 no further characters in the sequence str1 satisfies? yes yes yes yes yes yes L17App3 23 Regular Expressions [a-d[f-h]] matches any of a,b,c,d,f,g,h the union of a-d and f-h [^a-d] matches any character that is NOT a, b, c, d, \d matches any digit same as: [0-9] \s matches any whitespace character: same as: [ \t\n\x0b\f\r] vertical tab is \xob, aka \u000b \w matches any word character: same as: [a-za-z_0-9] L17App4 L17App5 L17App6 L17App7 24

Regular Expressions a* matches zero or more a s a+ matches 1 or more a s a? matches 0 or 1 a s a{n,m} matches at least n a s but not more than m a s 25 Regular Expressions suppose we prompt the user for a time, with the instructions that the time must be one of 3, 6, or 9 am or pm acceptable: 9 am, 3 pm not acceptable: 10 am, 3 um, 9am, 9:00 am construct a regex to match this [369] [ap]m suppose we want to allow the space to be optional acceptable: 9am, 12 am, 12pm not acceptable: 10am, 9:00am construct a regex to match this [369]?[ap]m or [369][ ]?[ap]m 26 Numeric Strings Using the Wrapper Classes String s = "1020"; int n1 = Integer.parseInt(s); long n2 = Long.parseLong(s); double n2 = Double.parseDouble(s); float n3 = Float.parseFloat(s); number to string conversions? best handled using the + operator Copyright 2006 Pearson Education Canada Inc. How to get primitive values from String objects suppose we have a sequence of characters suppose that sequences happens to be the same as a literal value from a primitive type e.g., 897 875l false C Use any of these static methods Integer.parseInt(str) Short.parseShort(str) Byte.parseByte(str) Long.parseLong(str) Double.parseDouble(str) Float.parseFloat(str) Boolean.parseBoolean(str) look at API, note the contract re: parameter java.lang.numberformatexception: Value out of range. L17App1b L17App1c 28

How to get primitive values from String objects suppose we have a one-character String and we want the corresponding char e.g., C d 9 there is a wrapper class Character(just like the others) unfo, there is no Character.parseCharacter(str) or other such static method instead: char c = C.charAt(0) 29