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.
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
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
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
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
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
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 :
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
endWith() method test a string and return true if that end with the given character, otherwise it returns false.
Syntax :
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
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
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