• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Strings




String Handling
Performing various operation on String value is known as String handling.In java language String can be handled with the help of following predefined classes.
1.String class
2.StringBuffer class
3.StringBuilder class
String Class:
It is also predefiend class in java Language in java.lang package. Whose object is immutable( Value can not be changed )
Syntax:
String s="Value";
or
String objref=new String("Value");
Example:
Datatype:
String s1="Sateesh";
Class
String s2=new String("Sateesh");
String class offers following methods to perform various operations on String value.
String Methods:
1.String Length
2.String CharAt
3.String CompareTo
4.String CompareToIgnoreCase
5.String Concat
6.String StartsWith
7.String EndsWith
8.String Equals
9.String EqualsIgnoreCase
10.String IndexOf
11.String LastIndexOf
12.String SubString
13.String ToLowerCase
14.String ToUpperCase
15.String Trim

StringBuffer Methods
1.String Insert
2.String Delete
3.String Append
4.String Replace

StringBuilder Methods
Both String and StringBuffer Methods







String Length:

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());
}
}


Output:
7
String CharAt:

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


Output:
t
String CompareTo

Which can be used to compare two strings.This method returns an integer value . It returns zero both the strings same otherwise it returns non 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");
}
}
}


Output:
Both Are Not Equal
String CompareToIgnoreCase

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");
}
}
}


Output:
Both Are Equal
String Concat

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


Output:
Sateeshkumar
String StartsWith

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"));
}
}


Output:
true
String EndsWith

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"));
}
}


Output:
false
String Equals

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");
}
}
}


Output:
Both Are Not Equal
String EqualsIgnoreCase

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)


Output:
Both Are Equal
String IndexOf

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'));
}
}


Output:
3
String LastIndexOf

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'));
}
}


Output:
4
String SubString

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

Output: teesh

class Main
{
public static void main(String args[])
{
String s1=new String("sateesh");
System.out.println(s1.substring(2,5));
}
}


Output: tee
String ToLowerCase

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());
}
}


Output: sateeshm
String ToUpperCase

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());
}
}


Output: SATEESH
String Trim

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("Before Trim String length "+s1.length());
String s2=s1.trim();
System.out.println(s2);
System.out.println("After Trim String length "+s1.length());
}
}


Output: Before Trim String length 9
Sateesh
After Trim String length 7
Sateesh

String Buffer Class

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");

String Insert
which can be used to insert either a new characer or string at given index value.
Syntax:
sb.insert(index,'char');
sb.insert(index,"String");


class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.insert(7,'M'));
}
}

Output: sateeshM

class Main
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("sateesh ");
System.out.println(s1.insert(7,"kumar"));
}
}


Output:sateeshkumar
String Delete

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


Output: steesh
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).


Output: seesh
String Append

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"));
}
}


Output: sateesh kumar
String Replace

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"));
}
}


Output: skumar

String Builder Class
It is a same as StringBuffer class but only difference is StringBuffer is synchronized where as StringBuilder is non synchronized.
Syntax:
StringBuilder objref=new StringBuilder("Value");
StringBuilder supports all the methods of String and StringBuffer.


Java Training in Andhra Pradesh and Telangna

String Practice Pograms

Java Training in Andhra Pradesh and Telangna

1. Count vowels in a given string

2. Count spaces in a given string

3. Count Consonents in a given string

4. Count number of special characters in a given string

5. Remove vowels in a given string.

6. Remove Consonants in a given string.

6. Remove special characters in a given string.

8. Remove numbers in a given string.

9. How to print duplicate characters from the string?

10. How to check if two strings are anagrams of each other?

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

11. How to reverse a string in Java without using the reverse method?

12. How to count the occurrence of the given character in a string?

String : Sateesh
Character : 'e'
Count occurrence: 2

13. How to check if a string is a palindrome?

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.

14. How to count the number of words in a given string sentence?

15. How to reverse the words from the given string sentence?

String: MSK Technologies
OutPut: KSM seigolonhceT

16. How to swap two strings without using the third variable?

Before swapping....
The first String = one
The second String = two
After swapping....
The first String = two
The second String = one