String
String is a probably the most commonly used class in java library.string class is
Encapsulated under java. lang packages in java.every string that you create is
Actually an object of type string.one important thing to notice about string.
Object is that string objects are immutable that means once a string object is
created it can not be crated.
Java string provides a lot of cocepts that can be performed on a string such as
Compare, concat.in java string is basically an object that represent sequence of
Char values.
String str1=”hello”;
String class function
The methods specified below are some of the most commonly used
methods of the String class in Java. We will learn about each method
with help of small code examples for better understanding.
charAt() method
String charAt() function returns the character located at the specified
index.
public class Demo {
public static void main(String[] args) {
String str = "studytonight";
System.out.println(str.charAt(2));
}
, }
Output: u
NOTE: Index of a String starts from 0, hence str.charAt(2) means third
character of the String str.
equalsIgnoreCase() method
String equalsIgnoreCase() determines the equality of two Strings, ignoring
their case (upper or lower case doesn't matter with this method).
public class Demo {
public static void main(String[] args) {
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
}
}
Out put
True
indexOf() method
String indexOf() method returns the index of first occurrence of a
substring or a character. indexOf() method has four override methods:
int indexOf(String str): It returns the index within this string of the
first occurrence of the specified substring.
String is a probably the most commonly used class in java library.string class is
Encapsulated under java. lang packages in java.every string that you create is
Actually an object of type string.one important thing to notice about string.
Object is that string objects are immutable that means once a string object is
created it can not be crated.
Java string provides a lot of cocepts that can be performed on a string such as
Compare, concat.in java string is basically an object that represent sequence of
Char values.
String str1=”hello”;
String class function
The methods specified below are some of the most commonly used
methods of the String class in Java. We will learn about each method
with help of small code examples for better understanding.
charAt() method
String charAt() function returns the character located at the specified
index.
public class Demo {
public static void main(String[] args) {
String str = "studytonight";
System.out.println(str.charAt(2));
}
, }
Output: u
NOTE: Index of a String starts from 0, hence str.charAt(2) means third
character of the String str.
equalsIgnoreCase() method
String equalsIgnoreCase() determines the equality of two Strings, ignoring
their case (upper or lower case doesn't matter with this method).
public class Demo {
public static void main(String[] args) {
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
}
}
Out put
True
indexOf() method
String indexOf() method returns the index of first occurrence of a
substring or a character. indexOf() method has four override methods:
int indexOf(String str): It returns the index within this string of the
first occurrence of the specified substring.