String and String Buffers

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

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 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
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.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
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.
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.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
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.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
String handling. when you create a String object, you are creating a string that cannot be changed. That is, once a String object has been created, you.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: 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, Characters and Regular Expressions
Strings, StringBuilder, and Character
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String Handling in JAVA
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
Part a: Fundamentals & Class String
String and StringBuilder
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
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
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

String and String Buffers

Java implements strings as objects of type String. String object has been created, you cannot change the characters that comprise that string (immutable) immutable strings can be implemented more efficiently than changeable ones java provides two options: StringBuffer and StringBuilder. Both hold strings that can be modified after they are created. The String, StringBuffer, and StringBuilder classes are defined in java.lang All are declared final, which means that none of these classes may be subclassed

String pgm class MakeString { public static void main(String args[]) { char c[] = {'J', 'a', 'v', 'a'}; String s1 = new String(c); String s2 = new String(s1); System.out.println(s1); System.out.println(s2); }

Byte to String // Construct string from subset of char array. class SubStringCons { public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 }; } String s1 = new String(ascii); System.out.println(s1); String s2 = new String(ascii, 2, 3); // Adding new String content System.out.println(s2); This program generates the following output: ABCDEF CDE

String Length int length( ) s.length() Special String Operations String Literals char chars[] = { 'a', 'b', 'c' }; String s1 = new String(chars); String s2 = "abc"; System.out.println(s1); System.out.println(s2); String Concatenation String s = "He is " + age + " years old."; Str.concat(str);

Box(double w, double h, double d) { width = w; height = h; depth = d; String Conversion and toString( ) Every class implements toString( ) because it is defined by Object String toString( ) Box(double w, double h, double d) { width = w; height = h; depth = d; } public String toString() { return "Dimensions are " + width + " by " + depth + " by " + height + ".";

class Box { } double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; public String toString() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; class toStringDemo { public static void main(String args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System.out.println(b); // convert Box to string System.out.println(s); The output of this program is shown here: Dimensions are 10.0 by 14.0 by 12.0 Box b: Dimensions are 10.0 by 14.0 by 12.0

You can construct a String from a StringBuffer by using the constructor shown here: String(StringBuffer strBufObj) charAt( ) To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method char charAt(int where) char ch; ch = "abc".charAt(1); assigns the value “b” to ch. getChars( ) If you need to extract more than one character at a time, you can use the getChars( ) method getChars( ) If you need to extract more than one character at a time, you can use the getChars( ) method void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart) String s = "This is a demo of the getChars method."; s.getChars(start, end, buf, 0);

getBytes( ) There is an alternative to getChars( ) that stores the characters in an array of bytes byte[ ] getBytes( ) toCharArray( ) If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). char[ ] toCharArray( )

String Comparison equals( ) and equalsIgnoreCase( ) boolean equals(Object str) boolean equalsIgnoreCase(String str) String s1 = "Hello"; String s4 = "HELLO"; s1.equals(s4); equalsIgnoreCase  s1.equalsIgnoreCase(s4);

regionMatches( ) The regionMatches( ) method compares a specific region inside a string with another specific region in another string boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars) boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex,

s1.equals(s2) (s1 == s2) startsWith( ) and endsWith( ) boolean startsWith(String str) boolean endsWith(String str) "Foobar".endsWith("bar") "Foobar".startsWith("Foo") equals( ) Versu s1.equals(s2) (s1 == s2) It is important to understand that the equals( ) method and the == operator perform two different operations. As just explained, the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.

• lastIndexOf( ) Searching StringsoreCase(String str) indexOf( ) compareTo( ) Often, it is not enough to simply know whether two strings are identical int compareTo(String str) int compareToIgn Searching StringsoreCase(String str) indexOf( ) Searches for the first occurrence of a character or substring. • lastIndexOf( ) Searches for the last occurrence of a character or substring. Value Meaning Less than zero The invoking string is less than str. Greater than zero The invoking string is greater than str. Zero The two strings are equal.

int indexOf(int ch, int startIndex) int lastIndexOf(int ch, int startIndex) int indexOf(String str, int startIndex) int lastIndexOf(String str, int startIndex) s.indexOf('t') s.indexOf("the") s.indexOf('t', 10)

String substring(int startIndex) Modifying a String Because String objects are immutable, whenever you want to modify a String, you must either copy it into a StringBuffer or StringBuilder, or use one of the following String methods substring( ) String substring(int startIndex) String substring(int startIndex, int endIndex) org.substring(10, 20); concat( ) String concat(String str) String s1 = "one"; String s2 = s1.concat("two");

replace( ) String replace(char original, char replacement) String s = "Hello".replace('l', 'w'); String replace(CharSequence original, CharSequence replacement) trim( ) String trim( ) String s = " Hello World ".trim();

Data Conversion Using valueOf( ) The valueOf( ) method converts data from its internal format into a human-readable form. static String valueOf(double num) static String valueOf(long num) static String valueOf(Object ob) static String valueOf(char chars[ ]) Changing the Case of Characters Within a String String toLowerCase( ) String toUpperCase( ) s.toUpperCase() s.toLowerCase()

StringBuffer Constructors StringBuffer( ) StringBuffer(int size) StringBuffer(String str) StringBuffer(CharSequence chars) length( ) and capacity( ) The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method int length( ) int capacity( )

ensureCapacity( ) void ensureCapacity(int capacity) setLength( ) To set the length of the buffer within a StringBuffer object, use setLength( ). void setLength(int len)

charAt( ) and setCharAt( ) The value of a single character can be obtained from a StringBuffer via the charAt( ) char charAt(int where) void setCharAt(int where, char ch) append() StringBuffer append(String str) StringBuffer append(int num) StringBuffer append(Object obj)

insert( ) StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj) reverse( ) StringBuffer reverse( ) delete( ) and deleteCharAt( ) StringBuffer delete(int startIndex, int endIndex) StringBuffer deleteCharAt(int loc)

replace( ) StringBuffer replace(int startIndex, int endIndex, String str) StringBuilder J2SE 5 adds a new string class to Java’s already powerful string handling capabilities. This new class is called StringBuilder. It is identical to StringBuffer except for one important difference: it is not synchronized, which means that it is not thread-safe. The advantage of StringBuilder is faster performance. However, in cases in which you are using multithreading, you must use StringBuffer rather than StringBuilder.