0% found this document useful (0 votes)
5 views5 pages

stringsnotes

The document explains how to handle strings in Java using two predefined classes: String and StringBuffer. It outlines the creation, modification, and various methods associated with both classes, including examples for methods like toUpperCase, concat, equals, and more. Additionally, it highlights the differences between immutable strings created with the String class and mutable strings created with the StringBuffer class.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

stringsnotes

The document explains how to handle strings in Java using two predefined classes: String and StringBuffer. It outlines the creation, modification, and various methods associated with both classes, including examples for methods like toUpperCase, concat, equals, and more. Additionally, it highlights the differences between immutable strings created with the String class and mutable strings created with the StringBuffer class.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

String is set of charecters or words.

The strings in java can be handled with two


predefined
classes
1.String class 2. String Buffer

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.

2. StringBuffer Class: By creating an object for StringBuffer class string can be


created with
16 additional charecter space. Which can be modified later.

StringBuffer x= new StringBuffer(size);


StringBuffer x= new StringBuffer("String");

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.

The following are the methods of String class


1. toUpperCase : Converts all charecters under the string into Upper case
2. toLowerCase :Converts all charecters under the string into LowerCase.

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{

public static void main(String args[])


{
String s1="Ravi";
String s2=s1.concat("Krishna");
System.out.println(s1); Ravi
System.out.println(s2); RaviKrishna
}
}
4.equals : which will compare two given strings for equal and returns true if both
are equal
otherwise returns false.

5. equalsIgnoreCase : While comparing two strings ignores the case sensitivity.

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

ensureCapacity( ): It is used to increase the capacity of a StringBuffer object.


The new capacity will be set to either the value we specify or twice the current
capacity
plus two (i.e. capacity+2), whichever is larger. Here, capacity specifies the size
of the buffer.

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

You might also like