The java.lang.String class provides many useful methods for dealing with a sequence of char values. In this tutorial, you will see examples of using some of the methods.
Also, this tutorial is part 1 of the series. Read part 2 to learn about other methods that the String class provides.
char charAt(int index)
Returns the character at the specified index.
class Test {
public static void main(String[] args) {
String str = "alegru";
char c = str.charAt(2);
System.out.println(c);
}
}
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
class Test {
public static void main(String[] args) {
String str = "alegru";
int index = str.indexOf('l');
System.out.println(index);
}
}
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
class Test {
public static void main(String[] args) {
String str = "alegrutechblog";
int index = str.indexOf('l', 5); // search starts from char at index 5
System.out.println(index);
}
}
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
class Test {
public static void main(String[] args) {
String str = "alegrutechblog";
int index = str.indexOf("tech");
System.out.println(index);
}
}
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
int index = str.indexOf("tech", 10); // search starts from char at index 10
System.out.println(index);
}
}
int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
int index = str.lastIndexOf('e');
System.out.println(index);
}
}
int lastIndexOf(int ch, int fromIndex)
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
int index = str.lastIndexOf('e', 12); // search backward from char at index 12
System.out.println(index);
}
}
int lastIndexOf(String str)
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
int index = str.lastIndexOf("tech");
System.out.println(index);
}
}
int lastIndexOf(String str, int fromIndex)
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
int index = str.lastIndexOf("blog", 15); // search backward from char at index 15
System.out.println(index);
}
}
substring(int beginIndex)
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
String subString = str.substring(10);
System.out.println(subString);
}
}
substring(int beginIndex, int endIndex)
class Test {
public static void main(String[] args) {
String str = "alegrutechblogtechblog";
String subString = str.substring(10, 15);
System.out.println(subString);
}
}
char[] toCharArray()
import java.util.Arrays;
class Test {
public static void main(String[] args) {
String str = "alegru";
char[] charArray = str.toCharArray();
System.out.println(Arrays.toString(charArray));
}
}
toString()
class Test {
public static void main(String[] args) {
String str = "alegru";
System.out.println(str.toString()); // this is redundant
}
}
trim()
class Test {
public static void main(String[] args) {
String str = " alegru ";
String trimmedStr = str.trim();
System.out.println(trimmedStr);
}
}
concat(String str)
class Test {
public static void main(String[] args) {
String str = "alegru";
String newString = str.concat("techblog");
System.out.println(newString);
}
}