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

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

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

STRINGS AND STRINGBUILDERS. Spring 2019

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

String. Other languages that implement strings as character arrays

Creating Strings. String Length

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

String related classes

USING LIBRARY CLASSES

Java Foundations: Unit 3. Parts of a Java Program

STUDENT LESSON A10 The String Class

CST242 Strings and Characters Page 1

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

CS 1301 Ch 8, Part A

String Objects: The string class library

Programming with Java

Introductory Mobile Application Development


Lab 14 & 15: String Handling

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

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

CS251L REVIEW Derek Trumbo UNM

Programming Techniques

A couple of decent C++ web resources you might want to bookmark:

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

Strings! Today! CSE String Methods!

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

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

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

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

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

Introduction to Computer Programming

CS112 Lecture: Characters and Strings

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

More non-primitive types Lesson 06

Notes from the Boards Set BN19 Page

Lecture Notes for CS 150 Fall 2009; Version 0.5

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

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

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

CS106X Handout 04 Winter 2018 January 8 th, 2018 C++ Strings

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

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

Assignment 5: MyString COP3330 Fall 2017

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CPS 109 Lab 2 Alexander Ferworn Updated Fall 05. Ryerson University. School of Computer Science CPS109. Lab 2

AP Computer Science A

Searching and Strings. IST 256 Application Programming for Information Systems

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

Eng. Mohammed Abdualal

Intro to Computer Science II

Honors Computer Programming 1-2. Chapter 5 Exam Prep

Class. Chapter 6: Data Abstraction. Example. Class

Class Library java.lang Package. Bok, Jong Soon

Index COPYRIGHTED MATERIAL

Chapter 10: Text Processing and More about Wrapper Classes

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

Ohad Barzilay and Oranit Dror

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

You ll be reading more about tools for branching in Sections 3.4, and We are skipping Sections 3.5, 3.11 and 3.13-end of chapter.

Using System.out.println()

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

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

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

CS61B Lecture #2. Public Service Announcements:

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

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

Java Fall 2018 Margaret Reid-Miller

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

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

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

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

CSC 222: Object-Oriented Programming. Spring 2017

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Fall 2017 CISC124 9/27/2017

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

Unit 4: Classes and Objects Notes

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

WES-CS GROUP MEETING #9

Mutable and Immutable Objects

CS106X Handout 04 Autumn 2009 September 25 th, 2009 C++ Strings

CS 106A, Lecture 9 Problem-Solving with Strings

Fall 2017 CISC124 10/1/2017

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Various useful classes

Chapter 8: Strings and Things

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

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

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.

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

Program Fundamentals

CSC 222: Object-Oriented Programming. Spring 2013

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Transcription:

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

Strings in Java In Java, a string is an object. It is not a primitive type. The String class is used to create and store immutable strings. Immutable objects are objects that don t change once created. Kinda like final primitive types. Class StringBuilder creates objects that store flexible and changeable strings. We ll learn this later on in the course. Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 2 / 16

The String class Part of java.lang package 13 constructors and close to 50 methods String class API from java.oracle.com full listing of String class features Once you build a String object, it is fixed it cannot be changed. This is easier than it sounds. The only methods that can alter or set the instance variables are the constructors. All other methods that seem to change a string do so by returning a brand new String object You can assign a String reference variable to a new string, discarding the old one Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 3 / 16

A common way to construct a String One constructor allows the use of a string literal as the parameter. Example string constructions: String greeting = new String("Hello, World!"); String name = new String("Marvin Dipwart"); String subject = new String("Math"); // also, a shorthand notation for building strings String sentence = "The quick brown fox sat around for a while"; // this is not quite equivalent to using the //constructor above, but you still get a string //variable (which is what we care about right now) Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 4 / 16

Empty Strings The constructor with no parameters allows the building of an empty string: String s = new String(); // s refers to an empty string object Note that if you only declare a String variable, but you do not assign it to anything, it is not yet attached to any string: String s1; // s1 does not refer to any string yet Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 5 / 16

The equals() method equals() for comparing two strings (i.e. their contents), returns true or false if (str1.equals(str2)) System.out.print("The strings are the same"); equalsignorecase() - just like equals(), except that the case of the letters doesn t matter in making a match. For instance, Apple would be equal to apple with this method. Don t try to compare strings by using ==, <, >, etc. These would only compare the String reference variables, not the String objects themselves. Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 6 / 16

The compareto() method compareto() also for comparing two strings, good for sorting. if (str1.compareto(str2) <0) System.out.print("str1 comes before str2 in lexicographic ordering"); else if (str1.compareto(str2) == 0) System.out.print("str1 is the same as str2"); else if (str1.compareto(str2) >0) System.out.print("str2 comes before str1 in lexicographic ordering"); Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 7 / 16

What we know so far In Java, a string is an object. The String class is used to create and store immutable strings. Some String class methods we have used before: equals() for comparing two strings (i.e. their contents), returns true or false. equalsignorecase() - just like equals(), except that the case of the letters doesn t matter in making a match. compareto() also for comparing two strings, good for sorting. Don t try to compare strings by using ==, <, >, etc. These would only compare the String reference variables, not the String objects themselves. Other comparison methods include regionmatches, startswith, and endswith. See String class API for full details. Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 8 / 16

Concatenation concat() String concatenation. Returns a concatenation of two strings. String s1 = "Dog"; String s2 = "food"; String s3 = s1.concat(s2); //s3 now stores "Dogfood" //note: s1 and s2 are NOT changed The + symbol also performs String concatenation (as we ve already used in print statements). String s1 = "Cat"; String s2 = "nap"; String s3 = s1 + s2; //s3 now stores "Catnap" (s1, s2 unchanged) Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 9 / 16

Substrings substring() extracts part of a string and returns it. Takes in two parameters (begin index and end index) or 1 parameter (begin index). First character in a String has index 0. Substring returned is the index range [begin,end). Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 10 / 16

Substrings String s1 = "Hello, World"; String s2 = s1.substring(0,5);// s2 is now "Hello". // picks up indices 0-4 String s3 = s1.substring(0,7) + "Dolly"; System.out.print(s3);// prints "Hello, Dolly" System.out.print(s3.substring(4));//prints "o, Dolly" // can even use substring on string literals String s4= "What s up doc?".substring(10,13); // s4="doc" Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 11 / 16

String length length() returns a string s length (number of characters). String s1 = "Hello"; String s2 = "Goodbye world"; System.out.print(s1.length()); // output: 5 System.out.print(s2.length()); // output: 13 Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 12 / 16

charat() method charat() returns a specific character, given an index. String s1 = "Rumplestiltskin"; System.out.print(s1.charAt(0)); // output: System.out.print(s1.charAt(5)); // output: System.out.print(s1.charAt(12)); // output: R e k Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 13 / 16

Some Conversion methods tolowercase() returns all lower case version of string touppercase() returns all upper case version of string trim() returns a string that eliminates leading and trailing blank characters from original replace() returns a string with an old character replaced with a new one. old character and new character passed as parameters Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 14 / 16

Examples String s1 = "Zebra String s2 = s1.tolowercase(); // s2 is "zebra" String s3 = s1.touppercase(); // s3 is "ZEBRA" String s4 = " Apple "; String s5 = s4.trim(); // s5 is "Apple" String s6 = s5.replace( e, y ); // s6 is "Apply" Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 15 / 16

valueof() method valueof() there are several of these methods. They are static methods, and are used for converting other values to String objects int x = 12345; String s7 = String.valueOf(4.56); // s7 is "4.56" String s8 = String.valueOf(16); // s8 is "16" String s9 = String.valueOf(x); // s9 is "12345" Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 16 / 16