×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Method Overriding Java String Methods Part 2
Java String Methods Part 1 - Java
2876    Arnab De    25/07/2017

Java String

The String is not a data type in java. It is an object of String class that represents a sequence of character. Java string is same as an array of character in c. String can be declared in two way



String s=new String(“Tutorial @ Home”);

Or simply,

String s=”Tutorial At Home”;

We can also convert a character array into string

char[] ch={'T','u','t','o','r','i','a','l','@','H','o','m','e'};

String s=new String(ch);

In java library, numbers of built-in methods are available for handling a string. If we use those methods properly then any string problem can solve by the very small program.

charAt()

This method returns char value for the particular position or index with in a string. Here Index starts with 0.

Syntax : char charAt(int index)

public class CharAtDemo
{
public static void main(String []args)
{
String str="Tutorial At Home";
System.out.println("The character at position 5 is " + str.charAt(5));
}
}


Output:
The character at position 5 is i

length()

This method returns the length of the string.
Syntax : int length()

public class LengthDemo
{
public static void main(String []args)
{
String str="Tutorial At Home";
System.out.println("Length ialphabetslength());
}
}

Output:
Length is 16

trim()

The trim() is used to trimming the leading and trailing whitespace of a string (if exist).

Syntax: public Strinalphabets

character

class TrimExample
{
public static void main(String[] args)
{
String str = " Tutorial At Home is a educational website ";
String str1 = str.trim();
System.out.println(str1);
}
}

Output:
This is an example Java trim() Method

toUpperCase()

The toUpperCase() method is used to convert all the alphabets in a string to upper case.

Syntax: public String toUpperCase()

public class UpperCase
{
public static void main(String[] args)
{
String str = "Tutorial At Home";
String str1 = str.toUpperCase();
System.out.println("Result : " + str1);
}
}

Output:

Result : TUTORIAL AT HOME

toLowerCase()

The toLowerCase() method is used to convert all the alphabets in a string to lower case.

Syntax: String toLowerCase()

public class LowerCase
{
public static void main(String[] args)
{
String str = "Tutorial At Home";
String str1 = str.toLowerCase();
System.out.println("Result : " + str1);
}
}

Output:

Result : tutorial at home

startsWith()

startWith() method test a string and return true if that start with the given character, otherwise it returns false. We can also specify a start index. If we specify the same then the first character will calculate from that character.

Syntax : 

  • startsWith(String prefix)
  • startsWith(String prefix,int index)
public class StartWithExample
{
public static void main(String[] args)
{
String str = "Tutorial At Home"; 
System.out.println(str + " starts with Tuto? " + str.startsWith("Tuto"));
System.out.println(str + " starts with Home? " + str.startsWith("Home"));
System.out.println(str + " starts with Home from index 12? " + str.startsWith("Home",12));
}
}

Output:

Tutorial At Home starts with Tuto? true
Tutorial At Home starts with Home? false
Tutorial At Home starts with Home from index 12? true

endsWith()

endWith() method test a string and return true if that end with the given character, otherwise it returns false. 

Syntax : 

  • endsWith(String prefix)
  • endsWith(String prefix,int index)
public class EndsWithExample

public static void main(String[] args) 

String str = "Tutorial At Home"; 
System.out.println(str + " ends with Tuto? " + str.endsWith("Tuto"));
System.out.println(str + " ends with Home? " + str.endsWith("Home"));

}

Output:

Tutorial At Home ends with Tuto? false
Tutorial At Home ends with Home? true

public int indexOf(int ch)

indexOf() method is just opposite to charAt() method. It returns the index of the first position of the given character. We can also specify a start index. If we specify the same then the character searching starts from the given index to onwards.

Syntax : indexOf(int ch)

public class IndexOfExample
{
public static void main(String[] args)
{
String str = "Tutorial At Home";
System.out.println("First index of o is " + str.indexOf("o"));
System.out.println("Second index of o is " + str.indexOf("o",5));
System.out.println("Second index of o is " + str.indexOf( "o", str.indexOf("o") + 1)); 
}
}

Output:

First index of o is 3
Second index of o is 13
Second index of o is 13

lastIndexOf()

public int lastIndexOf(int ch)

lastIndexOf() method is same as indexOf() method but search the character from end of the string. It returns the index of the last position of the given character. We can also specify the last index. If we specify the same then the character searching will start from the length()-given index.

public class IndexOfExample
{
public static void main(String[] args)
{
String str = "Tutorial At Home";
System.out.println("last index of o is " + str.lastIndexOf("o"));
System.out.println("Second last index of o is " + str.lastIndexOf("o",4));
}
}

Output:

last index of o is 13
Second last index of o is 3

Java Method Overriding Java String Methods Part 2
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.
Albert Einstein
Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whol
Albert Einstein
2026
57.19
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43517
01/01/2018     36473
25/06/2018     35466
28/06/2017     34547
02/08/2017     32969
01/08/2017     27456
06/07/2017     27201
15/05/2017     26835
14/07/2017     22453
21/04/2018     21105
softetechnologies