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

Similar documents
Programming with Java

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

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

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

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

Creating Strings. String Length

STUDENT LESSON A10 The String Class

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

CS112 Lecture: Characters and Strings

String related classes

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Lab 14 & 15: String Handling

CHAPTER 6 MOST COMMONLY USED LIBRARIES

More on variables and methods

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

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

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

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

Eng. Mohammed Abdualal

Strings! Today! CSE String Methods!

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

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

CISC 370: Introduction to Java

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

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Review: Python Transi,on Warning

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

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

Lecture 8: The String Class and Boolean Zen

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

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

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Eng. Mohammed Abdualal

More non-primitive types Lesson 06

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Lecture Notes K.Yellaswamy Assistant Professor K L University

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Building Strings and Exploring String Class:

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

Getting started with Java

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

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

String. Other languages that implement strings as character arrays

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

Voice Application Specification. SBString

Chapter 4 Mathematical Functions, Characters, and Strings

CS 1301 Ch 8, Part A

Activity 9: Object-Oriented

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

Welcome to the Using Objects lab!

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

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

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

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

2 nd Week Lecture Notes

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Strings are actually 'objects' Strings

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

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

Key Differences Between Python and Java

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

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

AP Computer Science A

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

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

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

Intro to Computer Science II

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

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Activity 9: Object-Oriented

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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

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

What did we talk about last time? Examples switch statements

Variables, Constants, and Data Types

Mathematics for Computer Graphics - Lecture 12

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

Sprite an animation manipulation language Language Reference Manual

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Designing data types. Fundamentals of Computer Science I

Chapter 8: Strings and Things

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

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

Preview from Notesale.co.uk Page 9 of 108

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Mr. Monroe s Guide to Mastering Java Syntax

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.

Fall 2017 CISC124 9/16/2017

Chapter 2 Working with Data Types and Operators

+ Inheritance. n Superclass (base class) higher in the hierarchy. n Subclass (child class) lower in the hierarchy

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

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

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

CS 106A, Lecture 9 Problem-Solving with Strings

Fundamentals of Programming Session 4

Transcription:

Strings

About Strings Strings are objects, but there is a special syntax for writing String literals: "Hello" Strings, unlike most other objects, have a defined operation (as opposed to a method): " This " + "is String " + "concatenation" Strings can contain any character, but some of them must be escaped in order to write them in a literal \" stands for the double-quote (") character \n stands for the newline character \\ stands for the backslash (\)character Each of these is written as a two-character sequence, but represents a single character in the string

Useful String methods char charat(int index) Returns the character at the given index position (0-based) boolean startswith(string prefix) Tests if this String starts with the prefix String boolean endswith(string suffix) Tests if this String ends with the suffix String boolean equals(object obj) Tests if this String is the same as the obj (which may be any type; false if it s not a String) boolean equalsignorecase(string other) Tests if this String is equal to the other String, where case does not matter int length() Returns the length of this string; note that this is a method, not an instance variable

Some more String methods int indexof(char ch) Returns the position of the first occurrence of ch in this String, or -1 if it does not occur int indexof(char ch, int fromindex) Returns the position of the first occurrence of ch, starting at (not after) the position fromindex int lastindexof(char ch) Returns the position of the last occurrence of ch in this String, or -1 if it does not occur int lastindexof(char ch, int fromindex) Returns the position of the last occurrence of ch, searching backward starting at position fromindex

Yet some more methods String substring(int beginindex) Returns a new string that is a substring of this string, beginning with the character at the specified index and extending to the end of this string. String substring(int beginindex, int endindex) Returns a new string that is a substring of this string, beginning at the specified beginindex and extending to the character at index endindex - 1. Thus the length of the substring is endindex-beginindex String touppercase() Returns a new String similar to this String, in which all letters are uppercase String tolowercase() Returns a new String similar to this String, in which all letters are lowercase String trim() Returns a new String similar to this String, but with whitespace removed from both ends

Understanding index With charat(index), indexof(x), and lastindexof(x), just count characters (starting from zero) Similar to Python s indexing in a String With substring(from) and substring(from, to), it works better to count positions between characters Remember: from is inclusive, to is exclusive Eclipse example: StringIndices.java

The split method String[] split(string regex) Breaks the string up into an array of strings The parameter is a regular expression that defines what separates the strings For example, String s = "one, two, three"; String[] ss = s.split(", "); This assigns the array {"one", "two", "three"} to ss Regular expressions allow you to capture many possible strings with a single expression Regular expressions are powerful but are beyond the scope of this class Bottom line: use split the same way you did in Python

Strings are immutable A String, once created, cannot be changed None of the preceding methods modify the String, although several create a new String Statements like this create new Strings: mystring = mystring + anothercharacter; Creating a few extra Strings in a program is no big deal Creating a lot of Strings can be very costly (slow)

Comparing Strings If you write String s = "abc"; String t = "abc"; the compiler only creates the string "abc" once, and makes s and t both refer to this one string Hence, the test s == t will be true (it compares pointers) However, if you now write String u = "a" + "bc"; the test s == u will be false This is because they are different strings (different pointers) Moral: Use s.equals(u) for strings, not ==

Characters Remember that char is a primitive type in Java It is actually represented as an 8-bit number on the machine You can use chars as numbers (add, subtract, compare with ==, etc.) Viewing chars as numbers might be useful sometimes chars are written with single quotes in Java If you use double quotes, you get a String (even for a single character) Being a primitive type, char has no methods There exists a Character class with some static methods Will cover it if we need it Eclipse example: StringAndCharExamples.java