10x your development speed by memorising these Java functionsπ₯
β‘οΈ length function return length of String object
Example
class Factance {
public static void main(String[] args) {
String boost = "Java";
System.out.println("length() : "+boost.length());
}
}
length() : 10
β‘οΈ charAt function returns character at index location
Example
class Factance {
public static void main(String[] args) {
String boost = "Hello Java";
System.out.println("charAt(2) : "+boost.charAt(2));
}
}
chatAt(2) : l
β‘οΈ equals function returns true only and only if Object is String and having same values into it
Example
class Factance {
public static void main(String[] args) {
String factance = "Factance";
String writeIn = "WriteIn";
System.out.println("factance.equals(writeIn) : "+factance.equals(writeIn));
}
}
factance.equals(writeIn) : false
β‘οΈ equalsIgnoreCase function returns TRUE only and only if Object is String and having same values into it( doesn't matter LOWERCASE OR UPPERCASE)
Example
class Factance {
public static void main(String[] args) {
String writeIn = "WriteIn";
String write_In = "writewn";
System.out.println("writeIn.equalsIgnoreCase(write_In) : "+write_In.equalsIgnoreCase(writeIn));
}
}
writeIn.equalsIgnoreCase(write_In) : false
β‘οΈ replace function replaces part of String and return replaced String
Example
class Factance {
public static void main(String[] args) {
String writeIn = "Fact WriteIn";
//replacing Fact with Factance
String replaceFactance = writeIn.replace("Fact", "Factance");
System.out.println("replaced String : "+replaceFactance);
}
}
replaced String : Factance WriteIn
β‘οΈ startsWith function returns TRUE if String starts with given string
Example
class Factance {
public static void main(String[] args) {
String writeIn = "Factance WriteIn";
System.out.println("startsWith() : "+writeIn.startsWith("Factance"));
}
}
startsWith() : true
β‘οΈ endsWith function returns TRUE if String ends with given string
Example
class Factance {
public static void main(String[] args) {
String writeIn = "Factance WriteIn";
System.out.println("endsWith() : "+endsWith("Factance"));
}
}
endsWith() : false
β‘οΈ substring function returns part of string based on given index
βοΈ substring(5) - returns part of String from 5 to end
βοΈ substring(5, 8) - returns part of String from 5 to 8 index
Example
class Factance {
public static void main(String[] args) {
String writeIn = "Factance WriteIn";
System.out.println("substring(5) : "+writeIn.substring(5));
System.out.println();
System.out.println("substring(5,8) : "+writeIn.substring(5,8));
}
}
substring(5) : nce WriteIn
substring(5,8) : nce
β‘οΈ indexOf function search characters in entire string and return index location
βοΈ "Java".indexOf('a') - search 'a' in entire 'Java' String and return first occurrence of match .i.e 1
βοΈ "Java".indexOf('a', 2) - search 'a' in entire 'Java' String and return 2nd occurrence of match .i.e 3
Example
class Factance {
public static void main(String[] args) {
String writeIn = "Java";
System.out.println("indexOf() : "+writeIn.indexOf("a"));
System.out.println("");
System.out.println("indexOf(a,2) : "+writeIn.indexOf('a',2));
}
}
indexOf() : 1
indexOf(a,2) : 3
β‘οΈ split function divide entire String based on String characters and return array
Example
class Factance {
public static void main(String[] args) {
String factance = "Factance WriteIn CodeFactory Blog";
//divide above factance string by space
String[] factanceArray = factance.split(" ");
//Display
for(String value: factanceArray){
System.out.println(value);
}
}
}
Factance
WriteInCodeFactory
Blog