Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Java Programming Strings Chapter 7.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
String class. Method concat Create string object –> String st1, st2; Input character to string object –> st1=br.readLine(); st2= br.readLine(); Use method.
String and StringBuffer classes
Object-Oriented Java Programming
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Java – String Handling.
Lecture 07 String Jaeki Song.
Chapter 10 Thinking in Objects Part 2
Object Oriented Programming
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Object-Oriented Java Programming
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer

Fundamentals of Characters and Strings Characters “Building blocks” of Java source programs String Series of characters treated as single unit May include letters, digits, etc. Object of class String See API

String Constructors Class String provides eleven constructors String() ▴ Initializes a newly created String object so that it represents an empty character sequence. String(byte[] bytes) ▴ Constructs a new String by decoding the specified array of bytes using the platform's default charset. String(byte[] bytes, int offset, int length) ▴ Constructs a new String by decoding the specified subarray of bytes using the platform's default charset. String(byte[] bytes, int offset, int length, String charsetName) ▴ Constructs a new String by decoding the specified subarray of bytes using the specified charset. String(byte[] bytes, String charsetName) ▴ Constructs a new String by decoding the specified array of bytes using the specified charset.

String Constructors String(char[] value) ▴ Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. String(char[] value, int offset, int count) ▴ Allocates a new String that contains characters from a subarray of the character array argument. String(int[] codePoints, int offset, int count) ▴ Allocates a new String that contains characters from a subarray of the Unicode code point array argument. String(String original) ▴ Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. String(StringBuffer buffer) ▴ Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. String(StringBuilder builder) ▴ Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

String Constructors See example in Figure 29.1 Uses 4 different constructors Note how jGRASP denotes literal Strings

String Methods length, charAt and getChars Method length Determine String length ▴ Like arrays, String s always “know” their size ▴ Unlike array, String s do not have length instance variable Method charAt Get character at specific location in String Method getChars Get entire set of characters in String See example in Figure 29.2

Comparing Strings Comparing String objects Method equals Method equalsIgnoreCase Method compareTo Method regionMatches See example in Figure 29.3 == compares two Strings to see if they refer to the same object.equals() compares to Strings to see if they contain the same value

Locating Characters and Substrings in Strings See examples in Figures 29.4 and 29.5

Extracting Substrings from Strings Create String s from other String s Method substring See example in Figure 29.6

Concatenating Strings + is a concatenation operator for Strings, although it is implemented using a StringBuffer Method concat concatenates two String objects See example in Figure 29.7

Miscellaneous String Methods Miscellaneous String methods Return modified copies of String Return character array See example in Figure 29.8

String Method valueOf String provides static class methods Method valueOf ▴ Returns String representation of object, data, etc. See example in Figure 29.9

Class StringBuffer When String object is created, its contents cannot change Used for creating and manipulating dynamic string data ▴ i.e., modifiable String s Can store characters based on capacity ▴ Capacity expands dynamically to handle additional characters ▴ length is always <= capacity Uses operators + and += for String concatenation See API

StringBuffer Constructors Four StringBuffer constructors StringBuffer() ▴ Constructs a string buffer with no characters in it and an initial capacity of 16 characters. StringBuffer(CharSequence seq) ▴ Constructs a string buffer that contains the same characters as the specified CharSequence. StringBuffer(int capacity) ▴ Constructs a string buffer with no characters in it and the specified initial capacity. StringBuffer(String str) ▴ Constructs a string buffer initialized to the contents of the specified string. See example in Figure 29.10

StringBuffer Methods length, capacity, setLength and ensureCapacity Method length Return StringBuffer length Method capacity Return StringBuffer capacity Method setLength Increase or decrease StringBuffer length Method ensureCapacity Set StringBuffer capacity Guarantee that StringBuffer has minimum capacity See example in Figure 29.11

StringBuffer Methods charAt, setCharAt, getChars and reverse Manipulating StringBuffer characters Method charAt ▴ Return StringBuffer character at specified index Method setCharAt ▴ Set StringBuffer character at specified index Method getChars ▴ Return character array from StringBuffer Method reverse ▴ Reverse StringBuffer contents See example in Figure 29.12

StringBuffer append Methods Method append Allow data values to be added to StringBuffer See example in Figure 29.13

StringBuffer Insertion and Deletion Methods Method insert Allow data-type values to be inserted into StringBuffer Methods delete and deleteCharAt Allow characters to be removed from StringBuffer See example in Figure 29.14

Wrapper Class Character Treat primitive variables as objects Type wrapper classes Boolean Character Double Float Byte Short Integer Long See examples in Figures 29.15, 29.16, and 29.17

Class StringTokenizer Tokenizer Partition String into individual substrings Use delimiter Java offers java.util.StringTokenizer See example in Figure 29.18

Regular Expressions Sequence of characters and symbols Defines a set of possible inputs, generalizes a wildcard Predefined set of strings \d – any digit \D – any non-digit \w – any word character (letter, digit, underscore) \W – any non-word character \s – any whitespace character (space, tab, CR, LF, FF) \S – any non-whitespace character

Example Regular Expressions Square brackets can be used to match any enclosed character A range of characters can be specified using - The complement can be created by using ^ as the first character Examples [aeiou] will match any vowel [a-zA-Z] will match any letter [^aeiou] will match any non-vowel character

Example Regular Expressions * matches zero or more patterns + matches one or more patterns ? matches zero or one patterns {n} matches exactly n patterns {n,} matches up to n patterns {n,m} matches between m and n patterns () can be used to group specifications | is an or for patterns Examples A+ and A* will match AAA [0-9]A* will match 1, [0-9]A+ will not; both match 1AAA

Class Pattern and Class Matcher Class Pattern An immutable regular expression Class Match A regular expression matching operation See exmaples in Figures 29.20, 29.21, 29.23, 29.24