1 / 14

Working with string

Working with string. In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder

dillan
Télécharger la présentation

Working with string

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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.

  2. 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);

  3. 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.

  4. 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

  5. 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.

  6. 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 • } • }

  7. Pool of Interned String CD 100 a 1 300 ABCD AB ABCD 100 200 100 b 400 ABCD 100 200 c 500 100 d 400 500 f g e

  8. 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

  9. 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 intcompareTo(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;

  10. 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));//C index Of():- returns the index of first operands of a character or String within String. public intindexOf(char ch); Public intindexOf(String s); Public intlastIndexOf(Char ch); Public intlastindexOf(String s); Ex: String s=“ababcababc”; s.O.P(s.indexOf(b));// o/p 1 S.O.P(s.indexOf(“abca”)); o/p 2 s.o.p(s.lastIndexOf(‘c’)); o/p 9 s.o.p(s.lastindexOf(“cab”)); o/p 4

  11. 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;

  12. 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(inti); • Public static String valueOf(long l); • Public static String valueOf(float f); • Public static String valueOf(double d); • a program for depiction

  13. 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.

  14. 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(inti); • Public static String toString(float f);

More Related