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

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

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

Using Methods. More on writing methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

An Introduction to Processing

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

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

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

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

Programming with Java

Iteration in Programming

Using Java Classes Fall 2018 Margaret Reid-Miller

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

Ticket Machine Project(s)

STUDENT LESSON A10 The String Class

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

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

Activity 9: Object-Oriented

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

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

Activity 9: Object-Oriented

Full file at

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

Creating Strings. String Length

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

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

Lab 14 & 15: String Handling

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

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

More on variables and methods

Key Differences Between Python and Java

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

Some miscellaneous concepts

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

CS 106 Introduction to Computer Science I

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

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

Computational Expression

String related classes

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

Java Basic Datatypees

Getting started with Java

Eng. Mohammed Abdualal

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

COMP Primitive and Class Types. Yi Hong May 14, 2015

Intro to Computer Science II

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

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

AP Computer Science A

Strings! Today! CSE String Methods!

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Pace University. Fundamental Concepts of CS121 1

Lesson:9 Working with Array and String

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

Introduction to Programming Using Java (98-388)

More non-primitive types Lesson 06

CMPT 125: Lecture 3 Data and Expressions

Chapter 4 Mathematical Functions, Characters, and Strings

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

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

CST242 Strings and Characters Page 1

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

The Java Language Rules And Tools 3

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.

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Fall 2017 CISC124 9/16/2017

USING LIBRARY CLASSES

CS112 Lecture: Characters and Strings

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

COMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016

Lab5. Wooseok Kim

Week 6: Review. Java is Case Sensitive

4. If the following Java statements are executed, what will be displayed?

Basic data types. Building blocks of computation

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

Text. Text Actions. String Contains

Software and Programming 1

Computer Science 145 Midterm 1 Fall 2016

Language Reference Manual

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

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

CISC 370: Introduction to Java

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

COMP6700/2140 Data and Types

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

Values and Variables 1 / 30

STRINGS AND STRINGBUILDERS. Spring 2019

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Full file at

Language Reference Manual

Visual C# Instructor s Manual Table of Contents

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java Fall 2018 Margaret Reid-Miller

CS 106A, Lecture 9 Problem-Solving with Strings

Transcription:

Strings Strings and their methods Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Recap: Primitive Types Java programming language supports eight primitive data types. The char data type stores one single character which is delimited by single quotes( ) e.g. char letter = a ; Data Type Default Value byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char '\u0000' boolean false

Primitive Types: char // VALID USE char letter = n ; char letter = N ; //Assign n to the letter variable //Assign N to the letter variable // INVALID USE char letter = n; //ERROR no single quotes around n. char letter = n ; //ERROR double quotes around n. char letter = not ; //ERROR char can only hold one character. Source: Reas & Fry (2014)

Primitive Types: char char is a 16-bit Unicode character. It s values range: from '\u0000 (or 0) to '\uffff' (or 65,535) For example: A is \u0041 a is \u0061 http://en.wikipedia.org/wiki/list_of_unicode_characters

Processing example 6.12 - Alphabet This code uses the underlying Unicode value for A (i.e. \u0041 and adds one to it each time the for loop is iterated. As the for loop is iterated 26 times, and the starting value is A, our loop will print the alphabet to the console. Source: Reas & Fry (2014)

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Object types e.g. String Strings, which are widely used in Java programming, are a sequence of characters enclosed by double quotes ( ). In the Java programming language, a String is an object type. The Java platform provides the String class to create and manipulate strings. The most direct way to create a String is to write: String greeting = "Hello world!"; http://www.tutorialspoint.com/java/pdf/java_strings.pdf

Object types - String // VALID USE String str = I am a sentence ; //Assigns the full sentence to str variable. String word = dog ; //Assigns the word dog to the word variable. String letter = A ; //Assigns the letter A to the letter variable. // INVALID USE String letter = n; //ERROR no double quotes around n. String letter = n ; //ERROR single quotes around n; use double. string letter = n ; //ERROR String should have a capital S. Source: Reas & Fry (2014)

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Primitive 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.

Primitive types vs. object types With primitive type variables (e.g. int, float, char, etc) the value of the variable is stored in the memory location assigned to the variable. With object types, the variable holds the memory address of where the object is located not the values inside the object. This memory address is called a reference to the object.

Primitive types vs. object types String a; String b; Hello World! b = a; int a; 17 int b; 17

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Strings are objects Variables created with the String data type are called objects. Objects are software structures that combine variables with methods that operate on those variables e.g. every String object has a built-in method that can capitalise its letters.

Strings and Java s API This link is to Java s Application Programming Interface (API). https://docs.oracle.com/javase/8/docs/api/index.html?o verview-summary.html At the moment, we are interested in finding out more information on String, particularly its methods: https://docs.oracle.com/javase/8/docs/api/java/lang/str ing.html

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Strings and some API methods Return Type Method Name Description int length() Returns the length of this string. String tolowercase() Converts all of the characters in this String to lower case. String touppercase() Converts all of the characters in this String to upper case. String trim() Returns a string whose value is this string, with any leading and trailing whitespace removed. String Int substring(int beginindex, int endindex) compareto(string anothers tring) Returns a string that is a substring of this string. Compares two strings lexicographically. char charat(int index) Returns the char value at the specified index. https://docs.oracle.com/javase/8/docs/api/java/lang/string.html

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Strings and methods To use these built-in methods, we must first understand the difference between: Internal method calls and External method calls

Internal method calls void draw() { background(204); drawx(0); } to this method that exists in the same sketch. This is an internal method call void drawx(int gray) { stroke(gray); strokeweight(20); line(0,5,60,65); line(60,5,0,65); }

Internal method calls drawx(0) is a method call. The sketch has a method with the following signature: void drawx(int gray) The method call invokes this method. As the method is in the same sketch as the call of the method, we call it an internal method call. Internal method calls have the syntax: methodname ( parameter-list)

External method calls We want to check the length of this String: String name = Joe Soap ; Looking at the String API, we can see this method: ReturnType Method Description int length() Returns the length of this string. A call to a method of another object is called an external method call.

External method calls External method calls have the syntax: object.methodname ( parameter-list) To find out the length of this String: String name = Joe Soap ; We make the following external method call: name.length();

Dot Notation Methods can call methods of other objects using dot notation. This syntax is known as dot notation: object.methodname ( parameter-list) It consists of: An object A dot A method name The parameters for the method

Topics list Primitive Types: char Object Types: String Primitive Types versus Object Types Strings and Java API Strings and methods Method calls (internal, external, dot notation) Using String methods: some examples

Processing example 6.12, version 1 Printing the length of a String to the console.

Processing example 6.12, version 2 Printing the length of a String to the console.

Processing example 6.13 Converting a String to UPPERCASE and printing it to the console.

Processing example 6.14 Converting a String to lowercase and printing it to the console.

Processing example 6.15 Removing all the leading and trailing spaces in a String and printing it to the console.

Questions?

References Reas, C. & Fry, B. (2014) Processing A Programming Handbook for Visual Designers and Artists, 2 nd Edition, MIT Press, London.

Department of Computing and Mathematics http://www.wit.ie/