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

Similar documents
Programming with Java

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

String Theory. Chapter Cut and Paste

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

STUDENT LESSON A10 The String Class

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

String. Other languages that implement strings as character arrays

USING LIBRARY CLASSES

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

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

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

Creating Strings. String Length

Java Foundations: Unit 3. Parts of a Java Program

Index COPYRIGHTED MATERIAL

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Lecture Notes for CS 150 Fall 2009; Version 0.5

More non-primitive types Lesson 06

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

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

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

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

CST242 Strings and Characters Page 1

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

Language Reference Manual

CS251L REVIEW Derek Trumbo UNM

String related classes

Full file at

More on variables and methods

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

Using System.out.println()

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Class Library java.lang Package. Bok, Jong Soon

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

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

Lab 14 & 15: String Handling

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

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

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

Language Reference Manual

CMPT 125: Lecture 3 Data and Expressions

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

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

CS112 Lecture: Characters and Strings

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

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

AP CS Fall Semester Final

STRINGS AND STRINGBUILDERS. Spring 2019

Searching and Strings. IST 256 Application Programming for Information Systems

Introductory Mobile Application Development

Getting started with Java

Lecture 5. Assignments. Arithmetic Operations. Arithmetic Operations -Summary 1/24/18. Lab 3: Variables and operations. Read Sections

Introduction to Programming Using Java (98-388)

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

Using Java Classes Fall 2018 Margaret Reid-Miller

5/23/2015. Core Java Syllabus. VikRam ShaRma

Operators. Java operators are classified into three categories:

Strings, characters and character literals

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Eng. Mohammed Abdualal

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

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

Program Fundamentals

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

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

String Objects: The string class library

CSE 21 Intro to Computing II. JAVA Objects: String & Scanner

CHAPTER 6 MOST COMMONLY USED LIBRARIES

Class. Chapter 6: Data Abstraction. Example. Class

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

Unit 2: Java in the small

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Basic Computation. Chapter 2

BASIC ELEMENTS OF A COMPUTER PROGRAM

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.

CS 1301 Ch 8, Part A

Sprite an animation manipulation language Language Reference Manual

array Indexed same type

Ohad Barzilay and Oranit Dror

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Lab 3: Call to Order CSCI 2101 Fall 2017

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

Programming with Java

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

COMS 469: Interactive Media II

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

APPENDIX A: SUMMARY OF IMPORTANT JAVA FEATURES

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

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

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

Transcription:

1

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

The Empty String smallest possible string made up of no characters at all (length is 0) "" typically used when we want to build something from nothing 3

Multi-line Instructions This program will allow you to enter a message in Morse Code. To enter your message: Click the mouse quickly to generate a dot; Depress the mouse longer to generate a dash. 4

Long Strings Strings can be arbitrarily long String chapter in your Java text can be 1 big string Practical issue for long strings: Readability Might want line breaks in a string newline character \n 5

Printing Instructions 1. Series of 5 System.out.printlin instructions, or 2. Define String constant INSTRUCTIONS; print INSTRUCTIONS private static final String INSTRUCTIONS = "This program will allow you to enter a message in Morse code.\n" + "\n" + "To enter your message:\n" + "Click the mouse quickly to generate a dot;\n" + "Depress the mouse longer to generate a dash."; Note "\n" just has length one!! 6

Readability and Legality Java does not allow us to write a String literal with actual line breaks in it! System.out.println( "The message that you have entered contains is illegal characters that cannot be translated." ); System.out.println( "The message that you have entered contains " + is legal "characters that cannot be translated." ); 7

Strings are Objects String is a class, not a primitive type Java provides many methods for manipulating them compare with equals method find length with length method 8

Manipulating Strings Java also provides String literals and + operator special features because strings used in many programs 9

Many String Methods somestring.length() returns an int that is number of characters in somestring somestring.endswith( otherstring ) returns true if and only if otherstring is a suffix of somestring somestring.startswith( otherstring ) returns true if and only if otherstring is a prefix of somestring 10

Finding a Substring somestring.indexof( otherstring ) think of otherstring as a pattern to be found returns an int giving first index in somestring where otherstring found Example. if sentence is "Strings are objects in Java." and pattern is "in", then sentence.indexof(pattern) returns 3. 11

If sentence is "Strings are objects in Java." and pattern is "primitive type", then sentence.indexof(pattern) returns -1 12

Another indexof somestring.indexof( pattern, startindex) Searches for pattern in somestring, beginning at index given by startindex If somestring is "Strings are objects in Java." and pattern is "ing", then somestring.indexof( pattern, 0) returns 3 somestring.indexof( pattern, 5) returns -1 somestring.indexof( "in", 5) returns 20 13

Cutting and Pasting can paste strings together with concatenation operator (+) can also extract substrings somestring.substring( startindex, endindex ) returns substring of somestring beginning at startindex and up to, but not including, endindex Ex. If urlstring is http://www.cs.williams.edu urlstring.substring( 7, 10 ) returns "www" and urlstring.substring( 0, 7 ) returns http:// and urlstring.substring( 7, urlstring.length() ) returns www.cs.williams.edu. 14

Rules for substring startindex must be a valid index in the string endindex may not be greater than the length of somestring 15

Cut it up How would I turn all words in 1 string into an array of strings, where each element is 1 word? 16

17

Case Sensitivity somestring.indexof( "IN" ) yields -1 if somestring is "Strings are objects in Java." 18

Dealing with Lower and Upper Case sometimes useful and important to distinguish between lower and upper case sometimes not if "http://www.cs.williams.edu" in our history surely we want to recognize "HTTP://www.cs.williams.edu" as the same Note: part of URL after domain name may be case sensitive. Will ignore that here. 19

Methods for Handling Case somestring.equalsignorecase( otherstring ) returns true if somestring and otherstring are composed of the same sequence of characters ignoring diffs in case somestring.tolowercase() returns a copy of somestring with upper case chars replaced by lower case somestring.touppercase() 20

Trimming Strings often want to ignore leading and trailing blanks in a string http://www.cs.williams.edu vs. "http://www.cs.williams.edu " somestring.trim() returns a copy of somestring with white space removed from both ends 21

Comparing Strings equals and equalsignorecase somestring.compareto( anotherstring ) returns 0, if somestring and anotherstring are equal positive int, if somestring appears after anotherstring in lexicographic ordering negative int, if somestring appears before anotherstring in lexicographic ordering 22

Lexicographic Ordering if 2 strings are made up of alphabetic characters and both all lower case or upper case then lexicographic ordering = alphabetical ordering 23

StringBuffer Java Strings are immutable. StringBuffer is essentially a mutable String Various ways to construct them // empty with initial capacity 1000 StringBuffer urlstringbuffer = new StringBuffer(1000); // create StringBuffer from existing String StringBuffer urlstringbuffer = new StringBuffer (urlstring); Many useful methods (append, replace, delete) Some String methods missing (tolowercase, touppercase) 24

Characters Strings are sequences of characters Java data type char represents characters a primitive data type char literal written by putting character in single quotes 'a', 'A', '?', '7', '\n' Note: these are not the same as "a", "A", "?", "7", "\n" 25

Declaration and Use To declare variable letter of type char char letter; chars in Java represented internally as integers can perform arithmetic operations on them can compare them with operators like < and > 26

1. Determine whether a char represents a digit in the range 0-9. 1. e if ( mysterychar >= '0' && mysterychar <= '9') works because integers representing '0' to '9' are consecutive numbers 2. Determine whether mysterychar is a lower-case alphabetical character if ( mysterychar >= 'a' && mysterychar <= 'z') works because ints representing 'a' to 'z' are consecutive 27

Constructing Strings from chars can build a String from char components new String (characterarray) If example is the array of char 'a' 'n' ' ' 'e' 'x' 'a' 'm' 'p' 'l' 'e' then String astring = new String(example); creates the String "an example" 28

Extracting chars from Strings astring.charat( index ) returns the char at the specified index in astring If astring is "Coffee", then astring.charat(1) returns o' common use for charat: check whether the characters in a string have some property 29

Using charat Consider a medical record management program Want to treat weight as an int If weightfield is the weight text field: String weight = weightfield.gettext(); int weightvalue = Integer.parseInt(weight); But this only works if weight entered looks like an int 30

Given a string, how could we construct a character array? 31