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

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

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

Java Basic Datatypees

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

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

Creating Strings. String Length

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

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

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.

STUDENT LESSON A10 The String Class

An Introduction to Processing

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

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Activity 9: Object-Oriented

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

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

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

Imports. Lexicon. Java/Lespérance 1. PROF. Y. LESPÉRANCE Dept. of Electrical Engineering & Computer Science

Language Reference Manual

Intro to Computer Science II

Activity 9: Object-Oriented

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Getting started with Java

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Strings, Strings and characters, String class methods. JAVA Standard Edition

An overview of Java, Data types and variables

STRINGS AND STRINGBUILDERS. Spring 2019

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

Strings! Today! CSE String Methods!

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

AP Computer Science A

More on variables and methods

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Fundamentals of Programming. By Budditha Hettige

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

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

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

The student should be familiar with classes, boolean and String fields, relational operators, parameters and arguments.

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Key Differences Between Python and Java

Chapter 2: Using Data

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

Variables, Constants, and Data Types

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

0. What is the capital of Canada? (4pts) a. Toronto b. Quebec c. Ottawa d. Vancouver e. Calgary f. Not sure

Introduction to Java

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

CS 106 Introduction to Computer Science I

3 The Building Blocks: Data Types, Literals, and Variables

Chapter 2: Data and Expressions

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

2 rd class Department of Programming. OOP with Java Programming

Language Reference Manual

CMPT 125: Lecture 3 Data and Expressions

Introduction to Programming Using Java (98-388)

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

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

CSC Java Programming, Fall Java Data Types and Control Constructs

Classes and Objects Part 1

Basics of Java Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

COMP-202 Unit 5: Basics of Using Objects

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

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

C Sc 127A Practice Test 2 SL Name 150pts

Java characters Lecture 8

TEST (MODULE:- 1 and 2)

COMP String and Console I/O. Yi Hong May 18, 2015

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Selected Questions from by Nageshwara Rao

Iteration in Programming

The Java Language Rules And Tools 3

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

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

More Sophisticated Behaviour

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

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

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

CST141 Thinking in Objects Page 1

Chapter 2: Data and Expressions

Java: Learning to Program with Robots

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Chapter 2: Data and Expressions

Prelim One Solution. CS211 Fall Name. NetID

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java Overview An introduction to the Java Programming Language

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Transcription:

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

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

h=ps://docs.oracle.com/javase/tutorial/java/data/manipstrings.html Strings: index of characters A String holds a sequence of characters. The index of the first character in a String is 0. The index of the last character in a String is length()-1.

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

h=ps://docs.oracle.com/javase/tutorial/java/data/manipstrings.html String methods: charat(int index) The following code gets the character at index 9 in a String: String anotherpalindrome = "Niagara. O roar again!"; char achar = anotherpalindrome.charat(9); Indices begin at 0, so the character at index 9 is 'O'

Processing example 7.1 Finding the character located a specific posi<on in a String.

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

h=ps://docs.oracle.com/javase/tutorial/java/data/manipstrings.html String methods: substring(int beginindex, int endindex) This method returns a new String that is a substring of this String. The substring begins at the specified beginindex and extends to the character at index endindex 1. String anotherpalindrome = "Niagara. O roar again!"; String roar = anotherpalindrome.substring(11, 15); This code returns a substring ( roar ) from anotherpalindrome. It extends from index 11 up to, but not including, index 15.

Processing example 7.2, version 1 Prin<ng out a substring of a String to the console.

Processing example 7.2, version 2 Prin<ng out a substring of a String to the console.

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

h=ps://docs.oracle.com/javase/tutorial/java/data/ String methods: compareto int compareto (String anotherstring) This method compares two strings lexicographically i.e. based on the Unicode value of the characters in the String. It returns an integer indica<ng whether this string is: greater than (result is > 0) equal to (result is = 0) or less than (result is < 0) the argument, anotherstring.

Processing example 7.3 String str1 = "Dog"; String str2 = "Cat"; What will be printed to the console? Which boolean expression evaluates to true? if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.3 String str1 = "Dog"; String str2 = "Cat"; str1.compareto(str2) returns a posi<ve integer as Dog (str1) comes aier Cat (str2). if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.4 String str1 = cat"; String str2 = "Cat"; What What will be will printed be printed to the to console? the console? Which boolean expression evaluates to true? if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.4 String str1 = cat"; String str2 = "Cat"; str1.compareto(str2) What will be printed returns to a posi<ve integer the console? as cat (str1) comes aier Cat (str2) in the Unicode character map. if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.5 String str1 = "Animal"; String str2 = "Cat"; What What will be will printed be printed to the to console? the console? Which boolean expression evaluates to true? if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.5 String str1 = "Animal"; String str2 = "Cat"; str1.compareto(str2) returns a nega<ve integer as Animal(str1) comes before Cat (str2) in the Unicode character map. if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.6 String str1 = Cat"; String str2 = "Cat"; What will be printed to the console? Which boolean expression evaluates to true? if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Processing example 7.6 String str1 = Cat"; String str2 = "Cat"; str1.compareto(str2) returns 0 as Cat (str1) is iden<cal to Cat (str2). if(str1.compareto(str2) < 0){ println(str1+" comes before "+ str2 +" in the alphabet"); else if(str1.compareto(str2) > 0){ println(str2 +" comes before "+ str1+" in the alphabet"); else{ println("the strings are iden<cal");

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

Recap: Object types e.g. String Strings are a sequence of characters enclosed by double quotes ( ). String is an object type. The Java API provides informa<on the String class and lists methods that can be used on Strings ( h=ps://docs.oracle.com/javase/8/docs/api/java/lang/ String.html). The most direct way to create a String is to write: String gree<ng = "Hello world!"; h=p://www.tutorialspoint.com/java/pdf/java_strings.pdf

Recap: Primi<ve types vs. object types String greeting; int i; 17 primitive type Hello World! String is an object type. The greeting variable contains a reference to where the String is stored in memory.

Recap: Primi<ve types vs. object types String a; String b; Hello World! b = a; int a; 17 int b; 17

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

String: Iden<ty vs Equality (1) if(input == "bye") {... tests identity i.e. the reference if(input.equals("bye")) {... tests equality Strings should always be compared using the.equals method

String: Iden<ty vs Equality (2) String input = "bye"; if(input == "bye") {... == tests identity :String "bye" :String ==? "bye" input

String: Iden<ty vs Equality (2) String input = "bye"; if(input == "bye") {... == tests identity :String "bye" == :String "bye" input (may be) false!

String: Iden<ty vs Equality (3) String input = "bye"; if(input.equals("bye")) {... equals tests equality :String "bye" equals :String "bye"? input

String: Iden<ty vs Equality (3) String input = "bye"; if(input.equals("bye")) {... equals tests equality :String "bye" equals :String "bye" input true!

Some common errors with Strings...

What s wrong here? void anymethod() { String str1 = "a"; String str2 = "b"; if(str1 == str2) { println(str1+" is the same as "+ str2); else { println(str1+" is NOT same as "+ str2);

Strings need to use the.equals method void anymethod() { String str1 = "a"; String str2 = "b"; if(str1 == str2) { println(str1+" is the same as "+ str2); else { println(str1+" is NOT same as "+ str2);

What s wrong here? public void anymethod() { int num1 = 1; int num2 = 2; if(num1 = num2) println(num1+" is the same as "+ num2); else println(num1+" is NOT same as "+ num2);

You need two equals for equality public void anymethod() { int num1 = 1; int num2 = 2; if(num1 = num2) println(num1+" is the same as "+ num2); else println(num1+" is NOT same as "+ num2);

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

null null is a special value in Java. All object variables are ini<alised to null. null means that the object variable does not have a reference e.g. str2 below. String str1; String str2; Hello World!

null null is a special value in Java All object variables are ini<alised to null. You can assign and test for null: private NumberDisplay hours; if(hours == null) {... hours = null;

Topics list Strings: index of characters String methods: charat(int index) substring(int beginindex, int endindex) compareto(string anotherstring) Recap: Primi<ve vs object String Iden<ty versus equality null Escape Sequences

Escape sequences When a String is printed, certain single characters that follow a backslash (\) have special meaning and the compiler interprets them accordingly. h=p://docs.oracle.com/javase/tutorial/java/data/characters.html

Java escape sequences Escape Sequence Descrip/on \t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point. \f Insert a formfeed in the text at this point. \' Insert a single quote character in the text at this point. \" Insert a double quote character in the text at this point. \\ Insert a backslash character in the text at this point. h=p://docs.oracle.com/javase/tutorial/java/data/characters.html

Examples of escape sequences print( Java\n ); is the exact same as: println( Java ); println( Java ); is similar to: println( \tjava ); h=p://docs.oracle.com/javase/tutorial/java/data/characters.html

Ques<ons?

Department of Compu<ng and Mathema<cs h=p://www.wit.ie/