Professional Documents
Culture Documents
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Characters Strings using
String Class String Buffer Class
Characters
An object of Character type contains a single character value. You use a Character object instead of a primitive char variable when an object is required -- for example, when passing a character value into a method that changes the value or when placing a character value into a data structure, such as a vector, that requires objects. Example : CharacterDemo.java
Strings
java.lang package, which does not require an import statement. A Java String is read-only and once created the contents cannot be modified. String class provides many operations for manipulating strings.
Constructors Utility Comparisons Conversions
public String( String value ) Constructs a new String that contains the same sequence of characters as the specified String argument. Example String s2 = new String( "abc" );
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String Constructors
Immutability
Characters in Strings can not be changed after the Strings are created Once created, a string cannot be changed: none of its methods changes the string. Such objects are called immutable. Immutable objects are convenient because several references can point to the same object safely: there is no danger of changing an object through one reference without the others being aware of the change.
Advantages Of Immutability
String Objects
String objects are immutable -- they cannot be changed once they have been created. References to string objects may be changed.
String str1 = new String (I like dogs.); String str2 = new String(I prefer cats.); str1 = str2; //reassign reference
String comparison
Equals String s1 = "Welcome"; String s2 = "welcome"; if (s1.equals(s2)) { // s1 and s2 have the same contents } if (s1 = = s2) { // s1 and s2 have the same reference }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String comparison
Use s1.compareTo(s2) return 0 if s1 is equal to s2 less than 0 if s1 is lexicographically less than s2 greater than 0 if s1 is lexicographically greater than s2 Example: s1="abc" and s2="abe" s1.compareTo(s2) return -2
String Concatenation
String s3 = s1.contact(s2); String s3 = s1 + s2;
String s3 =s1.concat(s2); concatenate s1 and s2 to s3 Alternatively, Use plus sign "+" to concatenate String myString=message+" and " + " HTML";
Substrings
String is an immutable class; its values cannot be changed individually. String s1 = "Welcome to Java"; String s2 = s1.substring(0,10) + "HTML"; public String substring(int beginIndex, int endIndex) return a substring from beginIndex to endIndex-1 public Stringsubstring(int beginIndex) return a substring from beginIndex to the end of the string
Example
classCheckString { public static void main(String[]args) { String statement="I like to check out a book now."; Stringst=statement.substring(0,22) +"magazine"+statement.substring(26); System.out.println(st); } }
StringBuffer Class
StringBuffer objects can be altered directly. A String object is always a fixed string. How to create StringBuffer objects?
StringBuffer string1 = Hello How are you;//not allowed StringBuffer string1 = new StringBuffer(Hello How are you);
StringBuffer contains a block of memory called buffer which may or may not contain a string and if it does, the string need not occupy all of the buffer. String buffers are preferred when heavy modification of character strings is involved (appending, inserting, deleting, modifying etc).
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
StringBuffer Constructors
The final class StringBuffer provides three constructors that create and initialize StringBuffer objects and set their initial capacity. StringBuffer(String s)
The contents of the new StringBuffer object are the same as the contents of the String object passed as argument. The initial capacity of the string buffer is set to the length of the argument string, plus room for 16 more characters.
StringBuffer(int length)
The new StringBuffer object has no content. The initial capacity of the string buffer is set to the value of the argument length, which cannot be less than 0.
StringBuffer()
This constructor also creates a new StringBuffer object with no content. The initial capacity of the string buffer is set for 16 characters.
No size Mentioned 0 16
No size Mentioned
0 16 16
6 22 50
BITS Pilani, Hyderabad Campus
Inserting charcters
Insert() method can be used for inserting characters Insert() method is also overloaded 1. StringBuffer insert(int index , String str); 2. StringBuffer insert(int index , char ch); 3. StringBuffer insert(int index, Object obj); << index >> must be within permitted range and should be positive
Jav a
Java 5
StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2, Programming "); JaProgrammingva System.out.println(strbuf); 15 System.out.println(strbuf.length()); StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2,new circle(10)); System.out.println(strbuf); System.out.println(strbuf.length());
CS/IS F213 First Semester 2012-13
?
BITS Pilani, Hyderabad Campus
class Test { public static void main(String args[]) { StringBuffer strbuf = new StringBuffer("Object");
strbuf.append(" oriented");
System.out.println(strbuf); strbuf.append(6.1); System.out.println(strbuf); } Object oriented
Object oriented6.1
StringBuffer Methods
Methods in StringBuffer
Set of Delimiters>>
Example
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC ; If Only Comma (,) is taken as delimiter: NO OF TOKENS = 4 Tokens : {BITS CS F213} {Object Oriented Programming} {Lecture Session 13; F-103} {LTC} If Only Semicolon (;) is taken as delimiter NO OF TOKENS = 2 Tokens : ?
If Both Comma(,) and Semicolon (;) are taken as delimiters: NO OF TOKENS = 5 Tokens : ? Delimiters themselves are not taken as
Tokens
StringTokennizer Constructors
1. 2. 3. 1. 2. 3. 4. StringTokennizer(String str) << str> String to be tokennized. No delimiter is specified so default set delimiters will beassumed. Delimiters will not be considered as tokens StringTokennizer(String str, String delimiters) << delimiters>> specify a delimiter String. Single or multiple characters can be specified as delimiters. If the delimiter String is :,; then colon, comma and semicolon are used as delimiters Individual character in the the delimiter String is separately treated as a delimiters. By Default Delimiters will not be considered as tokens
StringTokenizer Methods
1. Int countTokens()
Counts the number of tokens in StringTokennizer based upon delimiters
3. String nextToken()
Returns the next token in String form Used in conjunction with hasMoreTokens() method
4. Object nextElement()
Same as nextToken() but returns next Token in Object Form not in String Form
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC ; StringTokenizer strT1 = new StringTokenizer(str); // No Delimiters Specified System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken());
Number Of Tokens :3 Individual Tokens BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC ; StringTokenizer strT1 = new StringTokenizer(str,;); // Semicolon (;) is Specified as delimiter System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken());
Number Of Tokens :2 Individual Tokens BITS CS F213,Object Oriented Programming,Lecture Session 13 F-103,LTC
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC ; StringTokenizer strT1 = new StringTokenizer(str,,); // Comma (,) is Specified as delimiter System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken());
Number Of Tokens :4 Individual Tokens BITS CS F213 Object Oriented Programming Lecture Session 13;F-103 LTC
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC ; StringTokenizer strT1 = new StringTokenizer(str,;,); // Both Semicolon (;) and Comma (,) are Specified as delimiters System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken()); Number Of Tokens :5 Individual Tokens BITS CS F213 Object Oriented Programming Lecture Session 13 F-103 LTC
String str = "BITS CS F213,Object Oriented Programming,Lecture Session 13;F-103,LTC;BPHC,2012-13 ; StringTokenizer strT1 = new StringTokenizer(str,;,, true); // Both Semicolon (;) and Comma (,) are Specified as delimiters // Delimiters then selves are tokens now System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken());
Number Of Tokens :13 Individual Tokens BITS CS F213 , Object Oriented Programming , Lecture Session 13 ; F-103 , LTC ; BPHC , 2012-13
Example
//TestStringTokenizer.java: DemonstrateStringTokenizer import java.util.StringTokenizer; public classTestStringTokenizer { // Main method public static void main(String[]args) { // Create a string and stringtokenizer String s = "I am learning Java. Show me how to useStringTokenizer."; StringTokenizer st= newStringTokenizer(s); // Retrieve and display tokens System.out.println("The total number of words is " + st.countTokens()); while (st.hasMoreTokens()) System.out.println(st.nextToken()); } CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus }
Summary
In java strings are immutable objects. Use String buffer class to make the string mutable. The string buffer can be used when we want to modify stings. The string tokeneizer class allows us to break the given strings into tokens.