×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java String Methods Part 1 Java String Programs - Part 1
Java String Methods Part 2 - Java
2558    Arnab De    27/07/2017

substring()

This method is used to cut a portion of the string. substring method has two syntaxes.

Syntax: String substring(int startIndex)
Here we pass only one integer parameter, then the method returns a text starting from that index to end of the string.


public class SubString
{
public static void main(String []args)
{
String str="Tutorial At Home";
System.out.println("Part of the string is : " + str.substring(9));
}
}

Output:
Part of the string is : At Home


String substring(int startIndex,int endindex)
Here we pass two integer parameters, then the method returns a text starting from the first index to the end index end of the string.

public class SubString
{
public static void main(String []args)
{
String str="Tutorial At Home";
System.out.println("Part of the string is : " + str.substring(9,11));
}
}

Output:
Part of the string is : At

split()

split() is a very useful method in Java for handling the string. This method is used to split a string by a specified delimiter.

public class SplitTest
{
public static void main(String []args)
{
String str="Tutorial At Home";
String []words=str.split(" ");
for(int i=0;i<words.length;i++)
{
System.out.println("Word No " + (i+1) + " : " + words[i]);
}
}
}

Output:

Word No 1 : Tutorial
Word No 2 : At
Word No 3 : Home

join()

Join () method is just opposite of split (). It joins the sequence of strings or array of strings by using a specified delimiter.

public class JoinTest
{
public static void main(String []args)
{
String str="Tutorial At Home";
String []words=str.split(" ");
String res=String.join(" ",words);
System.out.println("Joined String is : " + res);
}
}

Output:

Joined String is : Tutorial At Home

equals()

The equals () is used for comparing two strings and return true, if the said strings are same. Otherwise, return false.

public class EqualsTest
{
public static void main(String[] args)
{
String str1 = "Tutorial At Home";
String str2 = "Tutorial At Home";
String str3 = "Tutorial At home";
System.out.println("\"Tutorial At Home\" equals \"Tutorial At Home\" : " + str1.equals(str2));
System.out.println("\"Tutorial At Home\" equals \"Tutorial At home\" : " + str1.equals(str3));
}
}

Output:

"Tutorial At Home" equals "Tutorial At Home" : true
"Tutorial At Home" equals "Tutorial At home" : false

equalsIgnoreCase()

The equalsIgnoreCase () is used for comparing two strings and return true, if the said strings are same. Otherwise, return false. This method is not case sensitive. That is, in this method "A" and "a" is same.

public class EqualsIgnoreCaseTest
{
public static void main(String[] args)
{
String str1 = "Tutorial At Home";
String str2 = "Tutorial At Home";
String str3 = "Tutorial At home";
System.out.println("\"Tutorial At Home\" equals \"Tutorial At Home\" : " + str1.equalsIgnoreCase(str2));
System.out.println("\"Tutorial At Home\" equals \"Tutorial At home\" : " + str1.equalsIgnoreCase(str3));
}
}

Output:

"Tutorial At Home" equals "Tutorial At Home" : true
"Tutorial At Home" equals "Tutorial At home" : true

replaceFirst(), replace() and replaceAll()

These methods are used to replace any sequence of characters by any other sequence of characters. Replace () and replaceAll () methods are same, But first, one replaces any character by another character and replace () replace one sequence of characters by another sequence of characters.

public class ReplaceTest
{
public static void main(String args[])
{
String str = "Tutorial At Home";
System.out.print("Return Value :" );
System.out.println(str.replaceFirst("At", "@"));
System.out.println(str.replaceFirst("a", "@")); 
System.out.println(str.replace("a", "@")); 
System.out.println(str.replaceAll("o", "@")); 
}
}

Output:

Return Value : Tutorial @ Home
Return Value : Tut@rial At Home
Return Value : Tut@rial At H@me
Return Value : Tut@rial At H@me

Java String Methods Part 1 Java String Programs - Part 1
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
Have love for everyone, no one is other than you.
Sri Sri Ramakrishna Paramahamsa
628
51.37
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41662
01/01/2018     35880
28/06/2017     33906
25/06/2018     33093
02/08/2017     32257
06/07/2017     26687
01/08/2017     26673
15/05/2017     26298
14/07/2017     21663
21/04/2018     20589
softetechnologies