Strings in Java
===========================================================
String represents group of characters.
In C, C++ languages, a string represents an array of characters, where the last character being '\0' represents the end of the string. But this is not valid
in Java, a string is an object of String class. It is not a character array. In java, we got character array also, but strings are given a different treatment
because of their extensive use on internet. JavaSoft people have created a class separately with the name 'String' in java.lang(language) package with
all necessary methods to work with strings.
Even though, String is a class, it is used often in the form of a data type
eg: String s="java";
Is String is class or datatype?
String is a class in java.lang package. But in java, all classes are also considered as data types. So we can take String as a data type also.
Can we call a class as datatype?
Yes, a class is also called 'user-defined' data type. This is because a user can create a class.
Creating Strings:
-----------------------
1.String S;
S="Hello";
2.String S="Hello";
3.String S=new String("Hello");
Character Array( to String object):
--------------------------------------------
char arr[]={'A','P','T','E','C','H'};
String S=new String(arr);
String S=new String(arr,2,4);
Example:I
----------------
//Using charAt()
//It returns character at given index
class A56
{
public static void main(String args[])
{
//charAt()
String empname=new String();
empname = "aptech computers";
System.out.println(empname.charAt(10));
}
}
//output is--- p
---------------------------------------------------------------------------------
Example II
-------------------------
//Using concat()
//It is used to concatinate two given strings
class A57
{
public static void main(String args[])
{
String empname=new String();
empname = "Aptech";
System.out.println(empname.concat(" Grafx"));
}
}
--------------------------------------------------------------------------------
Example III
-------------------------------
//Using compareTo()
//It returns the difference between two given strins based on ASCII values
class A58
{
public static void main(String args[])
{
String empname=new String();
empname = "Aptech";
System.out.println(empname.compareTo("aptech"));
}
}
--------------------------------------------------------------------------------
Example IV
------------------------------------
class StrDemo1
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String("hello");
//compareTo()
int S=S1.compareTo(S2);
if(S==0)
{
System.out.println("Both are Same..Difference is: "+S);
}
else
{
System.out.println("Both not are Same..The difference is: "+S);
}
//compareToIgnoreCase()
int SS=S1.compareToIgnoreCase(S2);
System.out.println("The Result of compareToIgnoreCase() is Both are Same: "+SS);
//indexOf()
int x=S1.indexOf('l');
System.out.println("Index position of 'l' is: "+x);
}
}
---------------------------------------------------------------------------
Example V
-----------------------------------
class StrDemo3
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String(" Hello World ");
System.out.println(S1.replace('H','Y'));
System.out.print(S2);
System.out.println(S1);
System.out.print(S2.trim());
System.out.println(S1);
}
}
---------------------------------------------------------------------------------
Example VI
-------------------------------------------
//Copying String to Character Array using getChars() method
class Strcpy
{
public static void main(String a[])
{
String str="Aptech Computer Education";
char arr[]=new char[20];
// copy from string to arr
str.getChars(7,15,arr,0);
System.out.println(arr);
}
}
//output is -- computer
---------------------------------------------------------------------------------
Example VII
-------------------------------------------
//using equals() method and == operator
class StrCmp
{
public static void main(String a[])
{
String s1="Hello";
//String s2="Hello";
String s2=new String("Hello");
//if(s1==s2)
if(s1.equals(s2))
{
System.out.println("Both are Same");
}
else
{
System.out.println("Not Same");
}
}
}
/* object reference:
-------------------------
Object reference is a unique hexadecimal number representing the memory adress of the object. It is useful to access the members of the object.
Difference between == and equals():
------------------------------------------------
== operator compares the references of the string objects. It does not compare the contents of the objects. equals() method compares the contents.
While comparing the strings, equals() method should be used as it yields the correct result.
String constant pool:
----------------------------
String constant pool is a separate block of memory where the string objects are held by JVM. If string object is created directly, using assignment
operator as: String s1="Hello", then is is stored in string constant pool.
*/
---------------------------------------------------------------------------------
Example VIII
-------------------------------------------
//Using split()
//It Converts the string into pieces
class Strsplit
{
public static void main(String a[])
{
String str="Aptech Computer Education";
String s[];
s=str.split(" ");
for(int i=0; i<s.length; i++)
System.out.println(s[i]);
}
}
Output
-------------
Aptech
Computer
Education
---------------------------------------------------------------------------------
Example IX
-------------------------------------------
/*
Objects are divided into mutable and immutable.
Mutable Objects are those objects whose contents can be modified.
Immutable Objects are those objects, once created can not be modified.
String class objects are immutable.*/
class StrImmut
{
public static void main(String a[])
{
String S1="Data";
String S2="Base";
S1=S1+S2;
System.out.println(S1);
}
}
---------------------------------------------------------------------------------
Example X
-------------------------------------------
/*
Differences between String and StringBuffer Class
------------------------------------------------------------------
String class objects are immutable, their contents cannot be modified. StringBuffer class objects are mutable, so they can modified.
*/
class StrBuff
{
public static void main(String a[])
{
StringBuffer sb=new StringBuffer("Data");
System.out.println(sb);
//append()
System.out.println(sb.append("Base"));
//indexOf()
int m=sb.indexOf("a");
System.out.println("The index position of 'a' is "+m);
//lastIndexOf()
int n=sb.lastIndexOf("a");
System.out.println("The last index position of 'a' is "+n);
//length()
System.out.println("The Length is " + sb.length());
//delete()
System.out.println("After Deleting: " + sb.delete(0,4));
//reverse()
System.out.println("The Reverse is " + sb.reverse());
//replace()
StringBuffer sb1=new StringBuffer("High Cost");
System.out.println(sb1.replace(0,4, "Low"));
//substring()
System.out.println("Substring of sb1 is: "+sb1.substring(4));
System.out.println("Substring of sb1 is: "+sb1.substring(0,3));
}
}
---------------------------------------------------------------------------------
Example XI
-------------------------------------------
//Palindrome or not
import java.io.*;
class StrBuff1
{
public static void main(String a[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String: ");
String str=br.readLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
System.out.println(sb);
str=sb.toString();
if(temp.equalsIgnoreCase(str))
{
System.out.println(temp+" is Palindrome");
}
else
{
System.out.println(temp+" is not a Palindorme");
}
}
}
---------------------------------------------------------------------------------
Example XII
-------------------------------------------
import java.io.*;
class StrBuff
{
public static void main(String a[]) throws Exception
{
StringBuffer sb=new StringBuffer();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Surname: ");
String sur=br.readLine();
System.out.print("Enter middle name: ");
String mid=br.readLine();
System.out.print("Enter Last Name: ");
String last=br.readLine();
//append
sb.append(sur +" ");
sb.append(last+ " ");
System.out.println("Name: "+sb);
//insert
int n=sur.length();
sb.insert(n,mid+" ");
System.out.println("Full Name: "+sb);
System.out.println(" In reverse: "+sb.reverse());
}
}
===========================================================
String represents group of characters.
In C, C++ languages, a string represents an array of characters, where the last character being '\0' represents the end of the string. But this is not valid
in Java, a string is an object of String class. It is not a character array. In java, we got character array also, but strings are given a different treatment
because of their extensive use on internet. JavaSoft people have created a class separately with the name 'String' in java.lang(language) package with
all necessary methods to work with strings.
Even though, String is a class, it is used often in the form of a data type
eg: String s="java";
Is String is class or datatype?
String is a class in java.lang package. But in java, all classes are also considered as data types. So we can take String as a data type also.
Can we call a class as datatype?
Yes, a class is also called 'user-defined' data type. This is because a user can create a class.
Creating Strings:
-----------------------
1.String S;
S="Hello";
2.String S="Hello";
3.String S=new String("Hello");
Character Array( to String object):
--------------------------------------------
char arr[]={'A','P','T','E','C','H'};
String S=new String(arr);
String S=new String(arr,2,4);
Example:I
----------------
//Using charAt()
//It returns character at given index
class A56
{
public static void main(String args[])
{
//charAt()
String empname=new String();
empname = "aptech computers";
System.out.println(empname.charAt(10));
}
}
//output is--- p
---------------------------------------------------------------------------------
Example II
-------------------------
//Using concat()
//It is used to concatinate two given strings
class A57
{
public static void main(String args[])
{
String empname=new String();
empname = "Aptech";
System.out.println(empname.concat(" Grafx"));
}
}
--------------------------------------------------------------------------------
Example III
-------------------------------
//Using compareTo()
//It returns the difference between two given strins based on ASCII values
class A58
{
public static void main(String args[])
{
String empname=new String();
empname = "Aptech";
System.out.println(empname.compareTo("aptech"));
}
}
--------------------------------------------------------------------------------
Example IV
------------------------------------
class StrDemo1
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String("hello");
//compareTo()
int S=S1.compareTo(S2);
if(S==0)
{
System.out.println("Both are Same..Difference is: "+S);
}
else
{
System.out.println("Both not are Same..The difference is: "+S);
}
//compareToIgnoreCase()
int SS=S1.compareToIgnoreCase(S2);
System.out.println("The Result of compareToIgnoreCase() is Both are Same: "+SS);
//indexOf()
int x=S1.indexOf('l');
System.out.println("Index position of 'l' is: "+x);
}
}
---------------------------------------------------------------------------
Example V
-----------------------------------
class StrDemo3
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String(" Hello World ");
System.out.println(S1.replace('H','Y'));
System.out.print(S2);
System.out.println(S1);
System.out.print(S2.trim());
System.out.println(S1);
}
}
---------------------------------------------------------------------------------
Example VI
-------------------------------------------
//Copying String to Character Array using getChars() method
class Strcpy
{
public static void main(String a[])
{
String str="Aptech Computer Education";
char arr[]=new char[20];
// copy from string to arr
str.getChars(7,15,arr,0);
System.out.println(arr);
}
}
//output is -- computer
---------------------------------------------------------------------------------
Example VII
-------------------------------------------
//using equals() method and == operator
class StrCmp
{
public static void main(String a[])
{
String s1="Hello";
//String s2="Hello";
String s2=new String("Hello");
//if(s1==s2)
if(s1.equals(s2))
{
System.out.println("Both are Same");
}
else
{
System.out.println("Not Same");
}
}
}
/* object reference:
-------------------------
Object reference is a unique hexadecimal number representing the memory adress of the object. It is useful to access the members of the object.
Difference between == and equals():
------------------------------------------------
== operator compares the references of the string objects. It does not compare the contents of the objects. equals() method compares the contents.
While comparing the strings, equals() method should be used as it yields the correct result.
String constant pool:
----------------------------
String constant pool is a separate block of memory where the string objects are held by JVM. If string object is created directly, using assignment
operator as: String s1="Hello", then is is stored in string constant pool.
*/
---------------------------------------------------------------------------------
Example VIII
-------------------------------------------
//Using split()
//It Converts the string into pieces
class Strsplit
{
public static void main(String a[])
{
String str="Aptech Computer Education";
String s[];
s=str.split(" ");
for(int i=0; i<s.length; i++)
System.out.println(s[i]);
}
}
Output
-------------
Aptech
Computer
Education
---------------------------------------------------------------------------------
Example IX
-------------------------------------------
/*
Objects are divided into mutable and immutable.
Mutable Objects are those objects whose contents can be modified.
Immutable Objects are those objects, once created can not be modified.
String class objects are immutable.*/
class StrImmut
{
public static void main(String a[])
{
String S1="Data";
String S2="Base";
S1=S1+S2;
System.out.println(S1);
}
}
---------------------------------------------------------------------------------
Example X
-------------------------------------------
/*
Differences between String and StringBuffer Class
------------------------------------------------------------------
String class objects are immutable, their contents cannot be modified. StringBuffer class objects are mutable, so they can modified.
*/
class StrBuff
{
public static void main(String a[])
{
StringBuffer sb=new StringBuffer("Data");
System.out.println(sb);
//append()
System.out.println(sb.append("Base"));
//indexOf()
int m=sb.indexOf("a");
System.out.println("The index position of 'a' is "+m);
//lastIndexOf()
int n=sb.lastIndexOf("a");
System.out.println("The last index position of 'a' is "+n);
//length()
System.out.println("The Length is " + sb.length());
//delete()
System.out.println("After Deleting: " + sb.delete(0,4));
//reverse()
System.out.println("The Reverse is " + sb.reverse());
//replace()
StringBuffer sb1=new StringBuffer("High Cost");
System.out.println(sb1.replace(0,4, "Low"));
//substring()
System.out.println("Substring of sb1 is: "+sb1.substring(4));
System.out.println("Substring of sb1 is: "+sb1.substring(0,3));
}
}
---------------------------------------------------------------------------------
Example XI
-------------------------------------------
//Palindrome or not
import java.io.*;
class StrBuff1
{
public static void main(String a[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String: ");
String str=br.readLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
System.out.println(sb);
str=sb.toString();
if(temp.equalsIgnoreCase(str))
{
System.out.println(temp+" is Palindrome");
}
else
{
System.out.println(temp+" is not a Palindorme");
}
}
}
---------------------------------------------------------------------------------
Example XII
-------------------------------------------
import java.io.*;
class StrBuff
{
public static void main(String a[]) throws Exception
{
StringBuffer sb=new StringBuffer();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Surname: ");
String sur=br.readLine();
System.out.print("Enter middle name: ");
String mid=br.readLine();
System.out.print("Enter Last Name: ");
String last=br.readLine();
//append
sb.append(sur +" ");
sb.append(last+ " ");
System.out.println("Name: "+sb);
//insert
int n=sur.length();
sb.insert(n,mid+" ");
System.out.println("Full Name: "+sb);
System.out.println(" In reverse: "+sb.reverse());
}
}