stringsnotes
stringsnotes
1. String Class : By creating object for the string class we can able to create
string. It can be
created with any of the following ways.
1. String s= "String"
2. String s= new String("String");
if we create a string with string class constant string will be created which can
not be modified
later.
The methods under string class can also be accessed with stringbuffer class but the
methods
under StringBuffer class can not able to acess with String class.
class ChangeCase
{
public static void main(String args[])
{
String s = "This is a test";
System.out.println(" Original :: " + s); This is a test
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println(" UpperCase :: " + upper); THIS IS A TEST
System.out.println(" LowerCase :: " + lower); this is a test
}
}
3. Concat : which will add specified string to the end of the specified string
(first);
class concat{
class equalsDemo
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-Bye";
String s4 = "HELLO";
System.out.println( s1.equals(s2) ); true
System.out.println( s1.equals(s3) ); false
System.out.println( s1.equals(s4) ); false
System.out.println(s1.equalsIgnoreCase(s4)); true
}
}
6. getChars: we can extract the specified number of charecters from the given
string.
we have to specify the starting index ,ending eindex,target array and from
position.
hello welcome to all
class getCharsDemo
{
public static void main(String args[])
{
String s = "This is a demo of the getChars Method";
int start = 1;
int end =35;
char buf[] =new char[end-start];
s.getChars(start,end,buf,0);
System.out.println(buf);
}
}
7.Trim : Which will eliminates the leading and trailing blank spaces of the given
string.
class A
{
public static void main(String [ ] a)
{
String s= " Welcome to all ";
s=s.trim();
System.out.println(s);
}
}
8. compareTo():
Which will compare two given string for equal and returns an integer value. If it
returns
positive number first string is big if it returns negitive number second string
is big
. if it returns 0 both are equal. Which will compare
lexicographicallly.
class cmp
{
public static void main(String [ ]x)
{
String s1=x[0]; xello A 65 a=97
String s2=x[1]; xez
int l=s1.compareTo(s2);
if(l>0)
System.out.println("First string is big");
else if(l<0)
System.out.println("Second String is big");
else
System.out.println("Both are equal");
}
}
StringBuffer Methods:
class cmp
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer(40);
int a=42;
ob=ob.append("a=").append(a).append("!");
System.out.println(ob);
}
}
a=42!
Ex 2:
class cmp
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("Hello");
System.out.println(ob.capacity()); 21
System.out.println(ob.length()); 5
ob.insert(5,"Welcome");
System.out.println(ob.capacity()); 21
System.out.println(ob.length()); 12
}
}
Ex: 3
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("This is a string");
ob.reverse();
System.out.println(ob);
}
}
Ex 4:
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("This were a string");
System.out.println(ob);
ob.replace(5,8," is ");
System.out.println(ob);
}
}
Ex 5:
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("This is a string");
ob.delete(5,6);
System.out.println(ob);
ob.deleteCharAt(0);
System.out.println(ob);
}
}
Ex 6:
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("Hello");
System.out.println(ob.charAt(0)); H
ob.setCharAt(1,"i"); Hillo
System.out.println(ob);
ob.setLength(2);
System.out.println(ob); Hi
}
}
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("Hello");
System.out.println(ob.indexOf('l')); 2
System.out.println(ob.lastIndexOf('l')); 3
}
}
class ABC
{
public static void main(String [ ]x)
{
StringBuffer ob= new StringBuffer("Hello");
System.out.println(ob.capacity()); 21
ob.ensureCapacity();
System.out.println(ob.capacity()); 44
}
}