charAt()
The charAt() method returns the character at the specified index in a string.The index of the first character is 0, the second character is 1, and so on.It throws IndexOutOfBoundsException - if index is negative or not less than the length of the specified string.
Syntax
public char charAt(int index)
public class Main {
public static void main(String[] args) {
String myStr = "Hello";
char result = myStr.charAt(0);
System.out.println(result);
}
}
// H
concat()
concat(String str)
It Concatenates the specified string to the end of this string.It returns a String, representing the text of the combined strings.
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
//John Doe
contains
(CharSequence s)
The contains() method checks whether a string contains a sequence of characters.It returns true if the characters exist and false if not. It throws NullPointerException - if the returned value is null.It return type is boolean.
String myStr = "Hello";
System.out.println(myStr.contains("Hel")); // true
System.out.println(myStr.contains("e")); // true
System.out.println(myStr.contains("Hi")); // false
isEmpty()
The isEmpty() method checks whether a string is empty or not. It returns true if, and only if, length() is 0.It return data type is boolean.
String myStr1 = "Hello";
String myStr2 = "";
System.out.println(myStr1.isEmpty());
System.out.println(myStr2.isEmpty());
//false
true
length()
It returns the length of this string.It returns value is int.
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println(txt.length());
//26
equals(Object anObject)
It Compares this string to the specified object.compares two strings, and returns true if the strings are equal, and false if not.
anObject = An Object, representing the other string to be compared
String myStr1 = "Hello";
String myStr2 = "Hello";
String myStr3 = "Another String";
System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal
System.out.println(myStr1.equals(myStr3)); // false
replace(char oldChar, char newChar)
The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));
//Heppo
split(String regex)
It splits this string around matches of the given regular expression. it returns String[].
regex= Required. A regular expression defining the separators where the string is split.
String myStr = "Split a string by spaces, and also punctuation.";
String regex = "[,\\.\\s]";
String[] myArray = myStr.split(regex);
for (String s : myArray) {
System.out.println(s);
}
//Split
a
string
by
spaces
and
also
punctuation
References
https://www.w3schools.com/java/ref_string_split.asp
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Top comments (0)