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

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

STRINGS AND STRINGBUILDERS. Spring 2019

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

String. Other languages that implement strings as character arrays

CST242 Strings and Characters Page 1

Lab 14 & 15: String Handling

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

String related classes

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

Creating Strings. String Length

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

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

USING LIBRARY CLASSES

Java Foundations: Unit 3. Parts of a Java Program

Programming with Java

Class Library java.lang Package. Bok, Jong Soon

Chapter 12 Strings and Characters. Dr. Hikmat Jaber


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

Index COPYRIGHTED MATERIAL

Introductory Mobile Application Development

Programming Techniques

CS251L REVIEW Derek Trumbo UNM

Lecture Notes for CS 150 Fall 2009; Version 0.5

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

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

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

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

C08c: A Few Classes in the Java Library (II)

Class. Chapter 6: Data Abstraction. Example. Class

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

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

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.

Assignment 5: MyString COP3330 Fall 2017

Ohad Barzilay and Oranit Dror

STUDENT LESSON A10 The String Class

String Objects: The string class library

More non-primitive types Lesson 06

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

Introduction to Computer Programming

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

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

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

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

Notes from the Boards Set BN19 Page

CS112 Lecture: Characters and Strings

Chapter 10: Text Processing and More about Wrapper Classes

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

Java Fall 2018 Margaret Reid-Miller

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

Intro to Computer Science II

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

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

CHAPTER 6 MOST COMMONLY USED LIBRARIES

TEXT-BASED APPLICATIONS

Searching and Strings. IST 256 Application Programming for Information Systems

Eng. Mohammed Abdualal

Prashanth Kumar K(Head-Dept of Computers)

Mutable and Immutable Objects

Remote Monitoring Example Remote RF Power Output Monitor

CC316: Object Oriented Programming

ECS-503 Object Oriented Techniques

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

Chapter 8: Strings and Things

Chapter 8 Strings. Chapter 8 Strings

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

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Unit 4: Classes and Objects Notes

Java String Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace,

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

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.

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

Using System.out.println()

Java Classes and Objects

AP Computer Science A

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Ruby: Useful Classes and Methods

APPENDIX A: SUMMARY OF IMPORTANT JAVA FEATURES

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

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

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

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

COMP 110/L Lecture 13. Kyle Dewey

Computing with Numbers

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

ADVANCED PROGRAMMING CONCEPTS

Object Oriented Software Design

Object Oriented Software Design

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

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Bellwork Strings 11/25/13

CS 106A, Lecture 9 Problem-Solving with Strings

Fall 2017 CISC124 10/1/2017

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

Page 1. Human-computer interaction. Lecture 1b: Design & Implementation. Building user interfaces. Mental & implementation models

CS1 Lecture 12 Feb. 11, 2019

Tha Java Programming Language

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

Transcription:

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

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.

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)

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

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"

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

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

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

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"

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"

The StringBuilder Class The StringBuilder class is a part of the java.lang package. A StringBuilder object is mutable (i.e. it can be changed). Three of the four constructors shown here. Here are sample creations: creates an empty string builder with initial capacity of 16 characters StringBuilder buf1 = new StringBuilder(); creates empty string builder with initial capacity given in parameter StringBuilder buf2 = new StringBuilder(50); creates string builder filled with argument initial capacity is length of given string plus 16 StringBuilder buf3 = new StringBuilder("Hello");

The append() method append() adds data to string in the builder object, at the end. Several versions for different parameter types (see API for full set) StringBuilder buf1 = new StringBuilder(); buf1.append("hello"); buf1.append(, ); buf1.append(" world!"); // buf1 is now "Hello, world!" buf1.append( ); buf1.append(123.45); // buf1 is now "Hello, world! 123.45"

The insert() method insert() insert data at a certain starting index. Like append, multiple versions for different types of data (see API for full set) StringBuilder buf2 = new StringBuilder(); buf2.append("welcome home"); // buf2 now "Welcome home" buf2.insert(8,"to my humble "); // buf2 = "Welcome to my humble home"

More StringBuilder methods delete() delete data from a string builder object StringBuilder buf3 = new StringBuilder("abcdefghijklm"); buf3.delete(4,9); // deletes indices 4-8. buf3 is now "abcdjklm" deletecharat() delete a character at specified index StringBuilder buf4 = new StringBuilder("abcdefg"); buf4.deletecharat(3); // buf4 is now "abcefg" buf4.deletecharat(1); // buf4 is now "acefg"

More StringBuilder methods reverse() reverses the contents of the string builder setcharat() sets a character at specified index (similar to deletecharat() capacity() returns current capacity of builder length() returns length of current string in builder (less than or equal to capacity) setlength() sets the exact length of the string in the builder to new value (parameter). This is the actual string, not the capacity. If the new length is smaller than previous length, characters are truncated from the string. If new length bigger, null characters are appended. charat() returns character at a specified index (parameter)