0% found this document useful (0 votes)
19 views13 pages

3.strings in Java-2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

By: Prof Pavan Malani

Strings is a combination of character .


Strings are the object of class string
There are 2 different ways to represent the string one using new operator and another using string literals.
The object create using string literals are stored in string constant pool while object created using new operator are stored
in heap memory
1)String s = "abc";
2)By using constructor
String s = new String ("abc");
Methods of a String Class :
1)toLowerCase() :
It converts method or string into lower case.
Eg. String s1 = "JAVA";
Sop ( s1. toLowerCase ()); o/p:java.
2) toUpperCase():
It converts string from lowercase to uppercase.
3) replace():
This method replace all the occurrences of first character with the second character.
Eg. String S1= " Java“;
Sop (s1.replace ('a','v')); O/p: Jvvv
4)length():
It calculates the length of invoking string.
Eg. String s1 =" Java"
Sop (S1.length()); o/p: 4
5) trim():
This method removes the whitespaces from the begining and the end of the string.
6)equals () :
It returns true if the both strings are equal otherwise it returns false.
Eg. S1 = " ABC “;
S2 = "abc" ;
S3 = "abc";
Sop(sl. equals (S2)); o/p: F
Sop(S2: equals (53)); o/p:T
7) equalsIgnoreCase():
This method is similar to equals method but only difference is that it ignores the cases.
Eg S1= "abc" ;
S2 = "ABC";
Sop (sl.equalsIgnoreCase (S2)); o/p: T
8)charAt():
This method is used to extract the character of specified position.
Eg. S1= "Java programming" ;
Sop (S1.charAt (2)); o/p: v
9)compareTo ():
It returns positive value if first String is greater than second, negative value if first
string is less than second and zero if both strings are equal
(Note: This method compares the ASCII value of letter in the string)
Eg. S1="abc";
S2 = "xyz" ;
int z =S1.compareTo(S2); -1
int z= S2.compareTo(S1); 1
S3= "axyz"
int z=S3.compareTo (S3); 0
10)concat():
It concatenate two string
11)startsWith():
This method check if the given string is starting with the specified character or string
returns boolean value true and false accordingly.
Eg. S1 =" Hello java";
Sop (Sl. startsWith ("Hel")); T
Sop (S1. startsWith("H")); T
Sop (Sl. startsWith ("PHP")); F
12)endsWith()
This method check if ends with particular string.
Eg S1 = "Hello java" ;
Sop( S1. endsWith ("va")); T
Sop (S1. endsWith ("a")); T
Sop (S1 endsWith (“PHP")); F
13)indexOf( ):
This method searches the 1st occurrence of the character in the invoking string .
14)lastIndexOf( ):
This method searches the last occurrence of given character in the string.
Eg. S1= “Java Programming” ;
S1.indexOf (‘a’); 1
S1.indexOf (‘a’, 3); 10
S1.lastIndexOf (‘g’); 14
15)substring( ):
This method will extract the number of character from the invoking string
Eg S1= “Java Programming” ;
sop(S1.substring (5)) o/p :programming
sop(S2.substring (5,9)) o/p :prog
16)split( ):
This method allows to separate the word of tokens from the invoking string according to
the given separator.
Eg s1= “rampur belapur Nagpur” ;
a1=s1.split (“ “);
a1[0]=rampur a1[1]=belapur a1[2]=Nagpur
a2=s1.split(“pur”):
a2[0]= ram a2[1]=bela a2[2]=Nag
class stringdemo String s3= "dot net";
{ int z=s2.compareTo (s3);
String s1 = "Java" ; if (z>0)
String s2 = "programming"; System.out.println("1st string greater than
2");
void conversion () else
{ if (z<0)
System.out.println (s1.toLowerCase ()); System.out.println("1st string less than
2nd");
System.out.println(s2.toUpperCase ());
else
System.out.println (s1.replace ('a', 'v'));
System.out.println ("Both string equals" );
System.out.println(s2.charAt(4));
System.out.println (s2.indexOf ('g'));
if (s1.equals (s2))
System.out.println (s2.lastIndexOf ('g'));
System.out.println ("Both strings are
equal"); System.out.println( s2.length());
else }
System.out.println ("Not equal"); public static void main(String args[])
{
stringdemo t=new stringdemo();
t.conversion ();
}
}
class vowel
System.out.println("No of vowels
{ "+cnt);
String s1="Java programing"; }
void count() public static void main(String args[])
{ {
int cnt=0; vowel t=new vowel();
for(int i=0;i<s1.length();i++) t.count();
{ }
char z=s1.charAt(i); }
if (z=='a'||z=='e'|| z=='i'
||z=='u'||z=='o')
cnt++;
}
class palin if (flag ==1)
{ System.out.println(" String not
String s1= “madam“; palindrome");
void check()
else
{
int flag=0;
System.out.println(" String
int j=s1.length()-1;
palindrome”);
for(int i=0;i<s1.length(); i++,j--) }
{ public static void main( String args[])
if(s1.charAt(i)!=s1.charAt(j)) {
{ palin t=new palin();
flag=1;
t.check ();
break;
} }
} }
class sort System.out.println (" Sorted Strings ");
{ for(i=0;i<len; i++)
String name[]= {"xyz","pqr","abc", “sss"} System.out.println(name[i]);
void operation() }
{ public static void main( String args[])
int len=name.length; {
for(int i=0; i<len; i++) sort t = new sort();
{ t.operation ();
for(int j=i+1 ; j< len ; j++) }
{ }
if(name [i].compareTo (name [j]) >0)
{
String t=name [i];
name[i]=name[j];
name[j] = t;
}
}
}
 String Class Works On Fixed Length of String while String Buffer
class is used to create string objects which can be modified.
 It represents the dynamic string whose character can be modified.
 All the methods of String class supported by String Buffer class
but it has some extra methods which are as follows.
Methods:
1)setLength(int n):sets the length of the string in string buffer. If
entered string is les than n character then it fills with null character
and if entered string is greater than n character then extra
character will be truncated.
2)setCharAt(n,’x’): it sets the character at given index
3)delete(int start,int end): deletes the character from start to end-1.
 4) insert(index,str): inserts the string at the specified positon.
 5)append(string str): appends the given string to the end of the
string.
 6)reverse(string): reverse the string.
 7)deleteCharAt(index): deletes the character at specified index.

Constructor:
StringBuffer s1=new StringBuffer();
Demo Code:
Class stringbuf
{
public static void main(String args[])
{
StringBuffer t=new StringBuffer(“Have Java”);
System.out.println(“Origional String is”+t);
t.insert(4,”a cup of”);
t.setCharAt(5,’*’);
t.append(“its fantastic”);
System.out.println(“after modification”+t);
System.out.println(“reverse of string is”+t.reverse());
}
}

You might also like