String
String is a class from java.lang package.
in case of c,c++ string represents a group of characters terminated by ‘\0’.
‘\0’ represents end of the string .
In java string is an object of String class and it is not a character array.
signature of String class:
public final class String extends Object implements Serializable, Comparable
class A extends String
{
}
//error
There are two ways to create a String in java
1)String s=”hello”;
In this case jvm creates an object in String constant pool and stores the hello in that object and it is referenced with s;
String constant pool is a seperate block of memory where the String objects are held by jvm.
if a String object is created directly using assignment operator(=) then it is stored in the string constant pool.
2)String s= new String(“hello”);
a object is created in the heap memory and it contains hello.
convert char array into String object using String class Constructor:
char a[]={‘h’,’e’,’l’,’l’,’o’};
String s= new String(a); // converting character array into String object
a object is created in the heap memory and it contains hello.
program 1:
public class SDemo
{
public static void main(String[] args)
{
String s=”hello”;
System.out.println(s);//s.toString()
String s1= new String(“hello”);
System.out.println(s1);//s1.toString()
char a[]={‘h’,’e’,’l’,’l’,’o’};
String s2= new String(a);//converting char array to String object
System.out.println(s2);//s2.toString()
System.out.println(s.toString());
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
—————————————————–
program 2:
== vs equals() vs equalsIgnoreCase()
== operator compares references
equals() compares content and it does case sensitive
equalsIgnoreCase() compares content and it does case in sensitive
public class SDemo1
{
public static void main(String[] args)
{
String s1=”hello”;
String s2=”hello”;
System.out.println(s1);//s1.toString();
System.out.println(s2);
System.out.println(s1==s2); //true
String s3=”rama”;
System.out.println(s1==s3);//false
String s4= new String(“config”);
String s5= new String(“config”);
System.out.println(s4==s5);//false
System.out.println(s4.equals(s5));//true
String s6=new String(“Config”);//C is in caps
System.out.println(s5.equals(s6));//false
System.out.println(s5.equalsIgnoreCase(s6));//true
}
}
—————————————–
immutability of Strings
Strings are immutable.
That is, whenever, we reassign a new value to a string,it goes to a new location and the old location is garbage collected.
strings cannot be changed in the same location.
//+ operator vs concat()
public class SDemo2
{
public static void main(String[] args) {
String s1=”manmohan”;
String s2=”sing”;
s1=s1+s2;
System.out.println(s1);// manmohan sing
System.out.println(s2);//sing
String s=”0″;
for(int i=1;i<10;i++)
{
s=s+i;
System.out.println(s);
}
String s3="config";
String s4=s3.concat("software solutions");
System.out.println(s3);//config
System.out.println(s4);//config software slutions
}
}
------------------------------------------------
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
//indexOf(),lastIndexOf(),charAt()
public class SDemo3
{
public static void main(String args[])
{
String str1 ="abcabcabc";
System.out.println(str1.indexOf('c')); // 2
System.out.println(str1.indexOf("bca"));// 1
System.out.println(str1.indexOf("bca",3));// 4
System.out.println(str1.indexOf('k'));// -1
System.out.println(str1.lastIndexOf('c'));//8
System.out.println(str1.lastIndexOf('k'));//-1
System.out.println(str1.charAt(2));//c
//System.out.println(str1.charAt(10));
//StringIndexOutOfBoundsException
}
}
------------------------------------------
length() ,contains()
public class SDemo4 {
public static void main(String[] args) {
String s1="rajiv";
String s2="h a";
System.out.println(s1.length());//5
System.out.println(s2.length());//3
String s3="config software solutions";
System.out.println(s3.contains("config"));//true
System.out.println(s3.contains("CONFIG"));//false
}
}
---------------------------------------------------
public class SDemo5
{
public static void main(String[] args) {
String str1 = "sleeping";
System.out.println(str1.startsWith("sl")); // true
System.out.println(str1.startsWith("pi",4)); // true
System.out.println(str1.endsWith("ing")); // true
System.out.println(str1.endsWith("hello")); //false
String str2="config software solutions";
System.out.println(str2.substring(7)); // software solutions
System.out.println(str2.substring(7,15)); // software 15 excluded
char ch[]=str2.toCharArray();
System.out.println(ch);
System.out.println(str2.length());
System.out.println(ch.length);
//System.out.println((ch.toString()).length());
}
}
-----------------------------------------------------------
//converting all primitive data types to String Object
public class SDemo6
{
public static void main(String args[])
{
//data types
int x = 10;
double d = 10.5;
float k = 10.6f;
boolean b = true;
String s1 = String.valueOf(x);
String s2 = String.valueOf(d);
String s3 = String.valueOf(k);
String s4 = String.valueOf(b);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
//System.out.println(s1*s1); error
}}
--------------------------------------
class SDemo7
{
public static void main(String args[])
{
String str1 = "java";
String str2 = "JAVA";
System.out.println(str1.toUpperCase());//JAVA
System.out.println(str2.toLowerCase());//java
System.out.println(str1);//java
System.out.println(str2);//JAVA
String str3=str1.toUpperCase();
System.out.println(str3);//JAVA
String str4=" hello ";//observe space before and after hello
System.out.println(str4.length());//7
System.out.println(str4.trim());//hello
System.out.println(str4.length());//7
String str5=str4.trim();
System.out.println(str5.length());//5
System.out.println(str4.trim().length());//5
}
}
---------------------------------------
public class SDemo8
{
public static void main(String[] args) {
String s="Config software solutions";
String s1[]=s.split(" ");
for(String s2:s1)
System.out.println(s2);
}
}
---------------------------------------
public class SDemo9
{
public static void main(String[] args) {
String s=String.format("%,d",12345678);
System.out.println(s);
String s1=String.format("%.2f",33345.6429);
System.out.println(s1);
}
}
-------------------------------------------------
StringBuffer
it is a class from java.lang
it is mutable in nature ie we can modify the string in the same location.
all the methods in StringBuffer are synchronized and it is thread safe.
signature:
public final class StringBuffer extends Object implements Serializable, CharSequence
StringBuilder
it is a class from java.lang introduced in 1.5 version
it is mutable in nature ie we can modify the string in the same location.
all the methods in StringBuilder are not synchronized and it is not thread safe.
-------------------------
//equals() in String class compares the content
//equals() in StringBuffer class compares the references
public class SBDemo {
public static void main(String[] args) {
String s1= new String("rama");
String s2= new String("rama");
System.out.println(s1.equals(s2));//true
StringBuffer s3= new StringBuffer("rama");
StringBuffer s4= new StringBuffer("rama");
System.out.println(s3.equals(s4));//false
}
}
--------------------------
public class SBDemo1
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer( );
StringBuffer sb2 = new StringBuffer(50);
StringBuffer sb3 = new StringBuffer("hello");
//to knowthe size of the buffer
System.out.println(sb1.capacity( )); // 16(default)
System.out.println(sb2.capacity( )); // 50
System.out.println(sb3.capacity( )); // 21 (16 + 5)
//to know the number of characters in the buffer
System.out.println(sb1.length( )); //0
System.out.println(sb2.length( )); //0
System.out.println(sb3.length( )); // 5
}
}
capacity( ) method gives the storage capacity of the buffer and length( ) method gives the number of characters present in the buffer. The default capacity is of 16 characters.
capacity( ) method does not exist in String class. But, length( ) exists in both String and StringBuffer class.
-----------------------------------------------------
public class SBDemo2
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer();
int x = 10;
sb1.append(x);
sb1.append("config software solutions");
sb1.append(10.5F);
sb1.append(10.6);
sb1.append(true);
System.out.println(sb1.length());
System.out.println(sb1.capacity());
}
}
--------------------------------
// Aim: to replace characters in a StringBuffer
//setCharAt( ) replaces a single character
//replace( ) replaces a group of characters
public class SBDemo3
{
public static void main(String args[])
{
StringBuffer sb1=new StringBuffer("hello there");
System.out.println(sb1);//hello there
//to replace a single character
sb1.setCharAt(0,'H');
sb1.setCharAt(6,'T');
System.out.println(sb1); // Hello There
StringBuffer sb2 = new StringBuffer("RamaKrishna");
sb2.replace(0,4,"Hello"); // 0,1,2,3 are replaced with Hello
System.out.println(sb2); //prints HelloKrishna
}
}
-------------------------------------------
// Aim: to insert a new string in the existing string in the buffer;inserting means original characters are not lost, but pushed.
public class SBDemo4
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Rama Rao");
System.out.println(sb1); //sb1.toString() Rama Rao
sb1.insert(4,"Krishna");
System.out.println(sb1); // RamaKrishna Rao
}
}
-------------------------------------------------
// Aim: to delete the characters in the exsting string
public class SBDemo5
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("rama");
System.out.println(sb1);// prints rama
// deleting a range of characters
sb1.delete(1,3); // 1,2 are deleted am
System.out.println(sb1); // prints ra, as am deleted
sb1.append("krishna");
System.out.println(sb1); // prints rakrishna
// to delete one character
sb1.deleteCharAt(1);
System.out.println(sb1);// prints rkrishna, as a is gone
StringBuffer sb2 = new StringBuffer("rama");
// to reverse the string
sb2.reverse( );
// the characters in the buffer itself are reversed and it is a permanent change
System.out.println(sb2); // prints amar
}
}
//reverse( ) is not a method of String class. So, do not apply to strings
----------------------------------------------------------------------------
// to illustrate pass by reference using string buffer.
public class SBDemo6
{
public static void display(StringBuffer sb2)
{
System.out.println(sb2); // Lord
sb2.append("Krishna");
System.out.println(sb2); // LordKrishna
}
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Lord");
System.out.println(sb1); // Lord
display(sb1);
System.out.println(sb1); // LordKrishna
}
}
--------------------------------------------------------------------
public class SBDemo7
{
public static void main(String args[])
{
String s1 = "radar";
StringBuffer sb1 = new StringBuffer(s1);//String to StringBuffer
sb1.reverse();
String s2 = sb1.toString();
if(s1.equals(s2))
System.out.println(s1+" is a palindrome");
else
System.out.println(s1+"is not a palindrome");
}
}
----------------------------------------------