Object Oriented Programming

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [2] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
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.
Java Programming Strings Chapter 7.
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
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,
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.
28-Jun-15 String and StringBuilder Part I: String.
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.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
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.
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)
Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
String Object String is a sequence of characters. Unlike many other programming languages that implements string as character arrays, Java implements strings.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
String class. Method concat Create string object –> String st1, st2; Input character to string object –> st1=br.readLine(); st2= br.readLine(); Use method.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
17-Feb-16 String and StringBuilder Part I: String.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
The String class is defined in the java.lang package
String and StringBuffer classes
Strings and Text Processing
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
The StringBuffer Class 
EKT 472: Object Oriented Programming
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
C++ STRINGS string is part of the Standard C++ Library
Chapter 9 Strings and Text I/O
String class and its objects
String and StringBuilder
String Handling String, StringBuffer, StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

Object Oriented Programming

Topics to be covered today String Buffer

String Buffer StringBuffer is a peer class of string that provides much of the functionality of Strings. String is immutable. StringBuffer represents growable and writable character sequence. StringBuffer may have characters and substring inserted in the middle or appended to the end. StringBuffer will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.

String Buffer Constructors Defines three constructors: StringBuffer() – default and reserves room for 16 characters. StringBuffer(int size) – accepts an integer argument that explicitly sets the size of the buffer StringBuffer(String str) – accepts a String argument that initially sets the content of the StringBuffer Object and reserves room for more16 characters.

Length() and capacity() Current length of a StringBuffer can be found via the length() method, while the total allocated capacity can be found through the capacity() method. General form: int length() int capacity()

Example: Length() and capacity()

ensureCapacity() Use ensureCapacity() to set the size of the buffer in order to preallocate room for a certain number of characters after a StringBuffer has been constructed. General form: void ensureCapacity(int capacity) Here, capacity specifies the size of the buffer. Usage: Useful if you know in advance that you will be appending a large number of small strings to a StringBuffer.

setLength() To set the length of the buffer within a StringBuffer object. General form: void setlength(int len) Here, len specifies the lenght of the buffer. Usage: When you increase the length of the buffer, null characters are added to the end of the existing buffer. If you call setLength() with a value less than the current value returned by length(), then the characters stored beyond the new length will be lost.

charAt() and setCharAt() To obtain the value of a single character, use CharAt(). To set the value of a character within StringBuffer, use setCharAt(). General form: char charAt(int where) void setCharAt(int where, char ch) For charAt(), where specifies the index of the characters being obtained. For setCharAt(), where specifies the index of the characters being set, and ch specifies the new value of that character.

Example: charAt() and setCharAt()

Getchars() To copy a substring of a StringBuffer into an array. General form: void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Where: srcBegin - start copying at this offset. srcEnd - stop copying at this offset. dst - the array to copy the data into. dstBegin - offset into dst.

Append() Concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. General form: StringBuffer append(Object obj) StringBuffer append(String str) StringBuffer append(int num) String.valueOf() is called for each parameter to obtain its string representation. The result is appended to the current StringBuffer object.

Example: append()

Insert() Inserts one string into another. It is overloaded to accept values of all the simple types, plus String and Objects. General form: StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj) Here, index specifies the index at which point the String will be inserted into the invoking StringBuffer object.

Example: insert()

reverse() To reverse the character within a StringBuffer object. General form: StringBuffer reverse() This method returns the reversed on which it was called. For example:

replace() Replaces one set of characters with another set inside a StringBuffer object. General form: StringBuffer replace(int startIndex, String endIndex, String str) The substring being replaced is specified by the indexes startIndex and endIndex. Thus, the substring at startIndex through endIndex-1 is replaced. The replacement string is passed in str. The resulting StringBuffer object is returned.

Example: replace

Substring() Returns a portion of a StringBuffer. General form: String substring(int startIndex) String substring(int startIndex, int endIndex) The first form returns the substring that starts at startIndex and runs to the end of the invoking StringBuffer object. The second form returns the substring that starts at startIndex and runs through endIndex-1. NOTE: These methods work just like those defined for String that were described earlier.

Questions?