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

Similar documents
STUDENT LESSON A10 The String Class

Strings! Today! CSE String Methods!

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

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

Lab 14 & 15: String Handling

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

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

Activity 9: Object-Oriented

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

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

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

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

Activity 9: Object-Oriented

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

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

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

Announcements. CompSci 100e Program Design and Analysis II

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

More on variables and methods

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

Creating Strings. String Length

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

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

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

Java Foundations: Unit 3. Parts of a Java Program

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

String. Other languages that implement strings as character arrays

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

Chapter 8: Strings and Things

More non-primitive types Lesson 06

CS112 Lecture: Characters and Strings

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

Eng. Mohammed Abdualal

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Bellwork Strings 11/25/13

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Strings, characters and character literals

Class. Chapter 6: Data Abstraction. Example. Class

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

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

Lecture 7. Strings & Stepwise Refinement

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

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

Javascript Methods. concat Method (Array) concat Method (String) charat Method (String)

Introduction to Programming (Java) 4/12

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

Midterms Save the Dates!

What did we talk about last time? Math methods boolean operations char operations

Programming with Java

String is a Class; Quoted Text is an Object

CSC 222: Object-Oriented Programming. Spring 2017

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

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

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Java Strings Java, winter semester

Chapter 1 Introduction to java

String Objects: The string class library

CSE 143 Au03 Midterm 2 Page 1 of 7

Assignment 5: MyString COP3330 Fall 2017

A+ Computer Science. strings

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

Language Reference Manual

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

Java Fall 2018 Margaret Reid-Miller

CHAPTER 6 MOST COMMONLY USED LIBRARIES

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

CSC 222: Object-Oriented Programming. Spring 2013

Introductory Mobile Application Development

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Lecture 4. Strings, Wrappers, & Containers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

ASSIGNMENT 4 Classes and Objects

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008)

Student Performance Q&A:

Designing data types. Fundamentals of Computer Science I

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.

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

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

Inf1-OP. Classes and Objects. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. November 23, School of Informatics

TEST (MODULE:- 1 and 2)

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Array. Prepared By - Rifat Shahriyar

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Software engineering as managing change. Inf1-OP. Data representation. How can we make change easier and cheaper?

Assessment - Unit 3 lessons 16-21

Netbeans tutorial:

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.

Using Java Classes Fall 2018 Margaret Reid-Miller

Activity 3: Better Keyword Detection Prepare Exploration: Using the API

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

COMP-202 Unit 5: Basics of Using Objects

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

CS 1301 Ch 8, Part A

Transcription:

Strings AP Computer Science Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

References & Objects

Strings are objects So far we have focused mainly on primitive data types (int, double, char). First, we will look at some basic differences between Strings and primitive data types. Strings are what we will start calling an object data type.

Initializing a String To declare and assign a value to a primitive we would do: int num = 5; We can declare and assign a value to a String in exactly the same way. However, there is another way to declare and assign a value to a String. String one = Hello World! ; String two = new String( Hello World! );

Initializing a String int num = 5; num 5 String one = Hello World! ; String two = new String( Hello World! ); one 123 123 Hello World! two 124 124 Hello World!

Initializing a String Reference variable one 123 123 Hello World! Reference String For a String the identifier is called a reference variable which stores a reference to a location in memory where the String is located.

References The best analogy I have heard of for the reference/string relationship is your cell phone number and you. Someone can find you and ask you to do something by calling your cell phone.

Strings

Strings Strings are immutable meaning you cannot change the String (the object) You can however re-assign a new String to a reference variable (the reference) You use indexes to access individual or groups of characters in a String 0 1 2 3 4 5 6 7 8 9 10 11 H e l l o W o r l d!

Using String Methods Proper syntax for calling a String method gives first the name of the String, a dot, the name of the method, and then any arguments stringname.methodname(arg1, arg2); String methods always return a new String (remember Strings are immutable!) Remember to assign the method to a variable

Concatenating Strings You can concatenate or add two Strings together using the + operator String word1 = We will play ; String word2 = with Turtles next! ; String word3 = word1 + word2; System.out.println(word3); We will play with Turtles next!

length() The length() method returns the number of characters in a String (including whitespace) Note the first index is 0 This means the index of the last character and the length are different String word = Hello ; int wordlength = word.length(); 5 System.out.println(wordLength); String wordtwo = Today is a good day! ; int wordtwolength = wordtwo.length(); System.out.println(wordTwoLength); 20

substring() substring() returns a section of the String You can combine length() and substring() String word = Hello World ; String newword = word.substring(6); System.out.println(newWord); World String word1 = Today is a ; String word2 = good day! ; String word3 = word1 + word2; int index = word2.length() + 1; System.out.println(index); out.println(word3.substring(index,15)); 11 good

charat() charat() returns a character at a certain index String wordtwo = Today is a good day! ; char chartwo = wordtwo.charat(11); System.out.println(charTwo); g How would you return the last char in a String? String word1 = We will play ; String word2 = with Turtles next! ;! String word3 = word1 + word2; out.println(word3.charat(word3.length()-1));

indexof() indexof() returns the index of a specified char o It reads left to right lastindexof() does the same except reads from right to left String word = Hello World ; int loc = word.indexof( o ); System.out.println(loc); 4 String wordtwo = Hello World! ; int locone = wordtwo.lastindexof( o ); System.out.println(locOne); 7

indexof() What happens when the char is not present in the String? When this happens, the value of -1 is returned to signify that the character is not in the String String word1 = We will play ; String word2 = with Turtles next! ; String word3 = word1 + word2; System.out.println(word3.indexOf( f )); -1

compareto() compareto() returns the difference in the Strings based upon the ASCII character values If the Strings are the same, you receive 0 If the first characters are the same, it continues checking to the right until it finds a difference One nice trick to keep track of whether the value returned is positive or negative is to imagine a - sign above the compareto() String word = Hello World ; 0 String newword = Hello World ; System.out.println(word.compareTo(newWord));

compareto() What is the output of these two sets of code? String word1 = hello World ; 32 String word2 = Hello World ; System.out.println(word1.compareTo(word2)); String word3 = Hello World ; -32 String word4 = Hello world ; System.out.println(word3.compareTo(word4));

equals() equals() tests for equality of two Strings equals() compares the actual Strings String word = Hello World ; String newword = Hello World ; true System.out.println(word.equals(newWord)); String word1 = Hello World ; String word2 = hello World ; false System.out.println(word2.equals(word1));

equals() This is distinctly different than = or as we will later see == == is actually comparing the reference String word = "Hello World"; String newword = "Hello World"; System.out.println(word==newWord); String word1 = "Hello World"; String newword1; newword1 = new String("Hello World"); System.out.println(word1==newWord1); true false

Returning non-string Values For the CodingBat.com labs today you will need to complete some return methods. The return type will be String, but many of you will want to include other values. Here is how you can fix this problem: public String add(string a, String b) { char one = a.charat(1); char two = b.charat(1); return + one + two; //adding the //empty string solves this problem }

String Methods length() substring(int from) Method Description Returns substring(int from, int to) Returns the length of this string (number of characters). Returns a section of the string starting at the location + 1 Returns a section of the string starting at the first location + 1 and including the second location charat(int index) Returns the char value at the specified index. char indexof(string str) lastindexof(string str) Returns the index within this string of the first occurrence of the specified substring. Returns the index within this string of the last occurrence of the specified substring. int String String compareto(string other) Compares two strings lexicographically. difference in ASCII values equals(string other) Compares this string to another String. true/false Here is the entire String library with all methods available. int int