Opening Hours :7AM to 9PM
Which can be used to find number of characters of given string.
Syntax:
s.length();
class Main
{
public static void main(String args[])
{
String s=new String("Sateesh");
System.out.println(s.length());
}
}
Which can be used to find the cheracter at the given index value.
Syntax:
s.charAt(indexvalue);
class Main
{
public static void main(String args[])
{
String s=new String("Sateesh");
System.out.println(s.charAt(2));
}
}
Which can be used to compare two strings.This method returns an integer value . It returns zero both the strings same otherwise it returns on zero integer value.
Syntax:
int n=s1.compareTo(s2);
class main
{
public static void main(String args[])
{
String s1=new String("sateesh");
String s2=new String("SATEESH");
int n=s1.compareTo(s2);
if(n==0)
{
System.out.println("Both Are Equal");
}
else
{
System.out.println("Both Are Not Equal");
}
}
}
Which also similar to compareTo method.But it is case sensitive method. that means both the strings are having same value with different case.That the JVM treats both are same strings.
Syntax:
int n=s1.compareToIgnoreCase(s2);
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
String s2=new String("SATEESH");
int n=s1.compareToIgnoreCase(s2);
if(n==0)
{
System.out.println("Both Are Equal");
}
else
{
System.out.println("Both Are Not Equal");
}
}
}
Which can be used to combine two strings into a single string.
Syntax:
s1.concat(s2);
class Main
{
public static void main(String args[])
{
String s1=new String("Sateesh");
String s2=new String("kumar");
System.out.println(s1.concat(s2));
}
}
Which returns true if the string is starts with given substring otherwise return false
Syntax:
s1.startsWith("substring");
class Main
{
public static void main(String args[])
{
String s1=new String("Welcome To Java Class");
System.out.println(s1.startsWith("Welcome"));
}
}
Which returns true if the string is ends with given substring otherwise return false
Syntax:
s1.endsWith("substring");
class Main
{
public static void main(String args[])
{
String s1=new String("Welcome To Java Class");
System.out.println(s1.endsWith("Welcome"));
}
}
Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.
Syntax:
s1.equals(s2);
Note:
It is case sensitive method.
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
String s2=new String("SATEESH");
if(s1.equals(s2))
{
System.out.println("Both Are Equal");
}
else
{
System.out.println("Both Are Not Equal");
}
}
}
Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.But here ignore case.
Syntax:
s1.equalsIgnoreCase(s2);
cclass Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
String s2=new String("SATEESH");
if(s1.equalsIgnoreCase(s2))
{
System.out.println("Both Are Equal");
}
else
{
System.out.println("Both Are Not Equal");
}
}
}
Note:
what is difference between equals() and == ?
equals always comes the content whereas == compare the reference;
Excample:
String s1="Hello";
String s2=new String("Hellow");
s1.equals(s2); -- true
s1==s2; -- false (references of both the strings different)
Which can be used to get the index value from the given string or character(first occurence)
Syntax:
s1.indexOf('character');
s1.indexOf("String");
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
System.out.println(s1.indexOf('e'));
}
}
Which can be used to get the index value from the given string or character(last occurence)
s1.LastIndexOf('character');
s1.LastIndexOf("String");
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
System.out.println(s1.lastIndexOf('e'));
}
}
Which can be used to get the substring or part of a string from the given string.
1.substring(index): usedto get the substring starting with the index value to last character.
Example:
s.substring(index);
2.substring("starting index,ending index): it can be used to get the specific value from given string.
Example:
s1.substring(starting index,ending index);
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
System.out.println(s1.substring(2));
}
}
class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
System.out.println(s1.substring(2,5));
}
}
Which can be used to convert upper case to lower case
Syntax:
s1.tolowerCase();
class Main
{
public static void main(String args[])
{
String s=new String("SATEESH");
System.out.println(s.toLowerCase());
}
}
Which can be used to convert lower case to upper case
Syntax:
s1.toUpperCase();
class Main
{
public static void main(String args[])
{
String s=new String("sateesh");
System.out.println(s.toUpperCase());
}
}
It is used to trim the starting and ending spaces from the given strings
Syntax:
s1.trim();
class Main
{
public static void main(String args[])
{
String s1=new String(" Sateesh ");
System.out.println(s1.length());
String s2=s1.trim();
System.out.println(s2.length());
}
}
it is a predefined class in java.lang package.which can be used to perform various operations on string .But whose object is mutable( content of the StringBuffer can be changed)
Syntax:
StringBuffer objref=new StringBuffer("Value");
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.insert(7,'M'));
}
}
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.insert(7,"kumar"));
}
}
Which can be used to delete either character or string from the main string.
Syntax:
1.deleteCharAt(index); // Delete One Character
2.delete(start index,ending index); Delete Multiple Characters
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.deleteCharAt(1);
}
}
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.delete(1,3));
}
}
Note:
In higher version of java delete(index) is replaced by deleteCharAt(index).
Which can be used to add a new String at the end of existing string.
Syntax:
append("String value");
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.append("kumar"));
}
}
Which can be used to replace a new String in to old string based on the index value.
Syntax:
replace(starting index,ending index,"new String")
class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.replace(1,7,"kumar"));
}
}
Java Training in Andhra Pradesh and Telangna
We can say if two strings are an anagram of each other if they contain the same characters but at different orders.
Example: army & mary
String : Sateesh
Character : 'e'
Count occurrence: 2
if the string is similar from the starting as well as the ending, then we can say that the string is a palindrome.
For example. madam, radar, etc.
String: MSK Technologies
OutPut: KSM seigolonhceT
Before swapping....
The first String = one
The second String = two
After swapping....
The first String = two
The second String = one