10 Java String functions make you a better developer

10 Java String functions make you a better developer

10x your development speed by memorising these Java functionsπŸ”₯

πŸ‘‰πŸ» length


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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


➑️ 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

 

Ejaz Shaikh

I'm passionate about freelancing, technology and healthy lifestyle. I enjoy writing code and writing about code.

Something awesome will come here..