String related classes

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

Lab 14 & 15: String Handling

Chapter 8 Strings. Chapter 8 Strings

Creating Strings. String Length

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

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

String. Other languages that implement strings as character arrays

Eng. Mohammed Abdualal

STRINGS AND STRINGBUILDERS. Spring 2019

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

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

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

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

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

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

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

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

Chapter 10: Text Processing and More about Wrapper Classes

Programming with Java

USING LIBRARY CLASSES

Class Library java.lang Package. Bok, Jong Soon

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

CST242 Strings and Characters Page 1

Strings! Today! CSE String Methods!

CHAPTER 6 MOST COMMONLY USED LIBRARIES

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

tostring() Method tostring() Method -Example tostring() Method -Example StringBufferClass StringBuffer class - Constructors

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

TEST (MODULE:- 1 and 2)

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.

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

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

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


CST141 Thinking in Objects Page 1

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

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

CS 1301 Ch 8, Part A

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

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

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

More non-primitive types Lesson 06

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

Today s plan Discuss the Bb quiz 1 Clarify Lab 1 Review basic Java materials Classes, Objects, Interfaces Strings Java IO. Understanding Board

Using Java Classes Fall 2018 Margaret Reid-Miller

STUDENT LESSON A10 The String Class

Lecture Notes K.Yellaswamy Assistant Professor K L University

Building Strings and Exploring String Class:

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

CS112 Lecture: Characters and Strings

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

Overview. Software Code is Everywhere. Software Crisis. Software crisis Development approaches Reusability Java packages API Sample classes:

Java Strings. Interned Strings. Strings Are Immutable. Variable declaration as String Object. Slide Set 9: Java Strings and Files

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

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

Fall 2017 CISC124 10/1/2017

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

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

Lesson:9 Working with Array and String

More on variables and methods

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

Introductory Mobile Application Development

Eng. Mohammed Abdualal

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

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

Object-Oriented Programming

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.

5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

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

Index COPYRIGHTED MATERIAL

Lesson 4 Utility classes: Math, String, I/O

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

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

Chapter 4 Defining Classes I

Chapter 11. Section 11.2

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

CS 106A, Lecture 9 Problem-Solving with Strings

CS251L REVIEW Derek Trumbo UNM

TEXT-BASED APPLICATIONS

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

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

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

CSE 21 Intro to Computing II. Post-Midterm Review

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

Case Study: Savings Account Interest

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

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

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

Chapter 4 Mathematical Functions, Characters, and Strings

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

ECS-503 Object Oriented Techniques

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

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Java: Learning to Program with Robots

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

Class. Chapter 6: Data Abstraction. Example. Class

Topics. Chapter 5. Equality Operators

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

Chapter 9 Strings and Text I/O

H212 Introduction to Software Systems Honors

Transcription:

Java Strings

String related classes Java provides three String related classes java.lang package String class: Storing and processing Strings but Strings created using the String class cannot be modified (immutable) StringBuffer class: Create flexible Strings that can be modified java.util package StringTokenizer class: Can be used to extract tokens from a String 2

String 3

String String class provide many constructors and more than 40 methods for examining in individual characters in a sequence You can create a String from a String value or from an array of characters. String newstring = new String(stringValue); The argument stringvalue is a sequence of characters enclosed inside double quotes String message = new String ( Welcome ); String message = Welcome ; 4

String Constructors 5

String Length Returns the length of a String length() Example: String s1= Hello ; System.out.println(s1.length()); 6

Extraction Get the character at a specific location in a string s1.charat(1) Get the entire set of characters in a string s1.getchars(0, 5, chararray, 0) 7

Extracting Substrings substring method enable a new String object to be created by copying part of an existing String object substring (int startindex) copies the characters form the starting index to the end of the String substring(int beginindex, int endindex) copies the characters from the starting index to one beyond the endindex 8

String Comparisons equals Compare any two string objects for equality using lexicographical comparison. s1.equals( hello ) equalsignorecase s1.equalsignorecase(s2) compareto s1.compareto(s2) s1 > s2 (positive), s1 < s2 (negative), s1 = s2 (zero) 9

String Comparisons 10

String Comparisons regionmatches compares portions of two String objects for equality s1.regionmatches (0, s2, 0, 5) s1.regionmatches (true, 0, s2, 0, 5) If the first argument is true, the method ignores the case of the characters being compared startswith and endswith check whether a String starts or ends with a specified String s1.startswith (s2) s1.endswith (s2) 11

String Concatenation Java provide the concat method to concatenate two strings. String s1 = new String ( Happy ); String s2 = new String ( Birthday ); String s3 = s1.concat(s2); s3 will be Happy Birthday 12

String Search Find the position of character/string within a String int indexof(char ch) int lastindexof(char ch) 13

String Split split() method splits a String against given regular expression and returns a character array String test = "abc,def,123"; String[] out = test.split(","); out[0] - abc, out[1] - def, out[2] - 123 14

String Conversions Generally, the contents of a String cannot be changed once the string is created, Java provides conversion methods touppercase() and tolowercase() Converts all the characters in the string to lowercase or uppercase trim() Eliminates blank characters from both ends of the string replace(oldchar, newchar) Replaces a character in the string with a new character 15

String to Other Conversions The String class provides valueof methods for converting a character, an array of characters and numeric values to strings valueof method take different argument types 16

String to Other Conversions Type To String From String boolean String.valueOf(boolean) Boolean.parseBoolean(String) byte String.valueOf(int) Byte.parseByte(String, int base) short String.valueOf(int) Short.parseShort (String, int base) Int String.valueOf(int) Integer.parseInt (String, int base) long String.valueOf(long) Long.parseLong (String, int base) float String.valueOf(float) Float.parseFloat(String) double String.valueOf(double) Double.parseDouble(String) 17

String Conversion Example To convert an int to a String (3 different ways): int n = 123; String s1 = Integer.toString(n); String s2 = String.valueOf(n); String s3 = n + ""; To convert a string to an int: String s = 1234 ; int n = Integer.parseInt(s); 18

StringBuffer 19

StringBuffer Can be used wherever a string is used More flexible than String Can add, insert, or append new contents into a string buffer The StringBuffer class has three constructors and more than 30 methods for managing the buffer and for modifying strings in the buffer Every StringBuffer is capable of storing a number of characters specified by its capacity 20

StringBuffer Constructors public StringBuffer() No characters in it and an initial capacity of 16 characters public StringBuffer(int length) No characters in it and an initial capacity specified by the length argument public StringBuffer(String string) Contains String argument and an initial capacity of the buffer is 16 plus the length of the argument 21

StringBuffer 22

StringTokenizer 23

StringTokenizer Break a string into pieces (tokens) so that information contained in it can be retrieved and processed Specify a set of characters as delimiters when constructing a StringTokenizer object StringTokenizer class is available since JDK 1.0 and the String.split() is available since JDK 1.4 StringTokenizer is a legacy class, retained for compatibility reasons, the use is discouraged! 24

StringTokenizer Constructors StringTokenizer(String str, String delim) StringTokenizer(String str) Methods hasmoretoken() Returns true if there is a token left in the string nexttoken() Returns the next token in the string nexttoken(string delim) Returns the next token in the string after reseting the delimiter to delim counttoken( ) Returns the number of tokens remaining in the string tokenizer 25

StringTokenizer 26