working with string in java four classes are provided to work with string:1) string 2)string buffer...

14
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 of String class represents Immutable sequence of characters. By immutable we menace that once a String object is created , it can’t be modifying .whenever an operation is performed on a string object that results in some changes a new

Upload: erik-black

Post on 13-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Working with stringIn java Four classes are provided to work with

String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder

Note: An object of String class represents Immutable sequence of characters. By immutable we menace that once a String object is created , it

can’t be modifying .whenever an operation is performed on a string object that results in some

changes a new string object is constructed.

Page 2: 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

Constructor of string:-• Public String();• Public string(char[]);• Public String(char[], int offset, int no of

characters);• Public String (byte[]);• Public string(byte[],int offset, int no of bytes);• Public String (String str);• Ex: char a[]={‘P’,’Q’,’R’,’S’,’T’};• Byte b[]={65,66,67,68,};• String s1=new String(a);

Page 3: 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

• String s2=new String(b);• String s3=new String(a,1,3);• String s4=new String(b,2,2);• String s5=new String(s1);• >print all string object s1,s2,s3,s4,s5.• Note: System.out.println(s1==s2); False• Note: system.out.println(s1==s5); False• Description:- equals to operators when use with

object comparers value of their reference variable not the contents of the objects.

Page 4: 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

• Equals method is used to content wise compare to objects of a class . This method is define in object class .

• Note: default implementation of equals methods compares the value of reference var.

• This method must be overridden in a class to facilitate content wise comparison .

• Note: String class Overrides equals methods.• Public boolean equals(Object o);• System.out.println(s1.equals(s2)); False• System.out.println(s1.equals(s5)); True

Page 5: 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

Advantages of immutable String • Advantage of strings being immutable is that they

can be shared . In java String literals and compilation time literals expressions having same character sequence are represented by single string object such a string object that has multiple reference in different scope is called interned String.

• Compiler create a separate tools of such strings.

Page 6: 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

program• Class Other• {• Public static string a=“ABCD”;• }• Class Test• {• Static String b=“ABCD”;• Public static void main(String arr[])• {• String c=“ABCD”;• String d=“AB”;• String e=d+”CD”;//String e=“AB”+”CD”;• String f=d+new String(“CD”);//create at runtime (In Heap)• String g=new String(c);// create at runtime (In Heap)• System.out.println(Other.a==b); True• System.out.println(b==c); True• System.out.println(c==e); True• System.out.println(e==f); False• System.out.println((Other.a.equals(b))); True• System.out.println(Other.a==g); False• }• }

Page 7: 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

Pool of Interned String

1

100

ABCD

100AB

200

a

d

e

b

c

100

100

100

200

CD

ABCD

ABCD

400

300

500

500

g

400

f

Page 8: 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

Method of string class • length():- returns the number of character in a strings.• Public int length();• charAt():-returns the character at the specified index from the

string ;• Public char charAt(int index);• getChars():-used to extracts characters from a string.• Public getChar (char[],int index,int no characters);• Ex:String s=LovingIndia• System.out.println(charAt(6));• Char a[]= new char[5];• S.getChars(a,6,5);• System.out.println(new String (a));• O/p I• O/p India

Page 9: 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

• toCharArray():- returns contents of the invoking string in the form of a character arrays.

• Public char[] toCharArray();• Char a[]=s.toCharArray();• compare To():- is used to find out the Sorting order of

two string, content wise compare of two String and returns zero if both string are same ,positive integer if invoking String comes after argument String in sorted order other wise represents negative integer .

• Public int compareTo(String s);• E.g. String s1=“CA”;• String s2=“DOG”;• S.o.p(s1.compareTo(s2)); C-D=-1;• S.o.p(s2.compareTo(s1)); D-C=1;

Page 10: 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

• substring():- is used to obtain a part of string .• 1)Public subString(int start index);• 2) public subString (int start index,int end index);• Ex: String s1=“ABCDEF”;• S.o.p(s1.subString(2));//CDEF• S.O.P(s1.subString(2,3));//Cindex Of():- returns the index of first operands of a character or String within

String. public int indexOf(char ch);Public int indexOf(String s);Public int lastIndexOf(Char ch);Public int lastindexOf(String s);Ex: String s=“ababcababc”;s.O.P(s.indexOf(b));// o/p 1S.O.P(s.indexOf(“abca”)); o/p 2s.o.p(s.lastIndexOf(‘c’)); o/p 9s.o.p(s.lastindexOf(“cab”)); o/p 4

Page 11: 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

toUpperCase():-convert lower case to Upper case.Public string toupperCase();toLowerCase():- convert to lowercase of upper case

string.Public string toLowerCase();Ex: string s=“abcd”;//s.toLowerCase();S.o.p(s); o/p abcd;// String t=s.toUpperCase();s.o.p(t); // o/p ABCD;

Page 12: 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

• valueOf():- is used to convert a primitive type into a string.

• Public static String valueOf(char ch);• Public static String valueOf(byte b);• Public static String valueOf(short s);• Public static String valueOf(int i);• Public static String valueOf(long l);• Public static String valueOf(float f);• Public static String valueOf(double d);• a program for depiction

Page 13: 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

• Class A• {• Public static void display(String a)• {• S.o.p(a);• }• }• Class Test• {• Public static void main(string arr[])• {• Int a=45;• Char c=‘A’;• Double d=45.67;• A.display(a);//A.display(String.valueOf(a));• A.display(c);//A.display(String.valueOf(c));• A.display(d);//A.display(String.valueOf(d));• }• }• O/P type mismatch Error or can not be applied to int,cahr,double.

Page 14: 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

Alternative method to convert primitive type into a String object.

• All the wrapper classes have static toString method is defined convert the primitive type into a string.

• Public static String toString(type value);• Actual methods:• Public static String toString(char ch);• Public static String toString(int i);• Public static String toString(float f);