To work with strings in java, we found many methods. These methods helps in different work. Let’s see below…

Find the string length

For finding the string length we use length () method.

class strLen{
    public static void main(String[] args) {
        String name=”apple”;
int n=name.length();
System.out.println(n);
 
        name=”microcodes”;
        n=name.length();
System.out.println(n);
    }
}
output

Get string variable data-type

 For this we need to treat that string variable as an object. We also need to use two methods – getClass() , getSimpleName().

public class strType {
    public static void main(String[] args) {
        String n=”dell”;
System.out.println(((Object)n).getClass().getSimpleName());
    }
}
output

String concatenation

In simple words, we can say concatenation means addition/summation of two or more operands.

public class conCatStr {
    public static void main(String[] args) {
        String m=”java”;
        String n=”microcodes”;
        String sum;
        // m.concat(n) ->concatinate two strings(m,n)
        sum=m.concat(n);
System.out.println(sum);
    }
}
output

Compare two strings

In java, comparing two strings can be done using relational operator(==). For this we need to use, .equals() method.

public class strComPare {
    public static void main(String[] args) {
        String a=”apple”;
        String b=”apple”;
        if (a.equals(b)) {
System.out.println(“both strings a,b matched!!”);
        }
        else{
System.out.println(“strings a,b not matched!!”);
        }
 
        String c=”Book”;
        String d=”book”;
        // c.euals(d) -> checks both string variables equals or not.
        if (c.equals(d)) {
System.out.println(“strings c,dnatched!!”);
        }
        else{
System.out.println(“strings c,d not matched!!”);
        }
    }
}
output

Get characters of strings using index

For getting any character using it’s index of a string, we use .charAt() method.

public class strAt {
    public static void main(String[] args) {
        String m=”Horse”;
System.out.println(m.charAt(0));
System.out.println(m.charAt(2));
System.out.println(m.charAt(3));
    }
}
output

** you will learn about index and it’s usage In array chapter.

Mutability

Java strings are immutable. It’s mean we can’t insert any new character/letter inside any string.

public class strMute {
    public static void main(String[] args) {
        String b=”cat”;
b.charAt(0)=’k’;
        // we try to insert letter ‘k’ at index 0 -> ‘c’
    }
}
output

Escape characters

In java, while working with string we can use escape characters like new-line character (\n), tab (\t) etc.

public class strEscape {
    public static void main(String[] args) {
        // \n create a new line
        String a=”java\npython”;
System.out.println(a);
        // \t keeps a tab between two letters
        String b=”java\tpython”;
System.out.println(b);
    }
}
output

Sub string

For getting a sub-string we need to use .substring(x,y). here x=starting index, y=ending index. Sub-string basically prints all the letters/characters between those ranges which mentioned there.

public class strSub {
    public static void main(String[] args) {
        String n=”python”;
System.out.println(n.substring(0,3));
        String m=”microcodes”;
System.out.println(m.substring(3,6));
    }
}
output

String contains() method

This method check mentioned letters/characters .

public class strContains {
    public static void main(String[] args) {
        String p=”windows”;
        if (p.contains(“dow”)) {
System.out.println(“letter found!!”);
        }
    }
}
output

Get string index

For getting any string’s character index we can use indexOf() method.

public class strIndex {
    public static void main(String[] args) {
        String name=”microcodes”;
System.out.println(name.indexOf(“r”));
System.out.println(name.indexOf(“m”));
    }
}
output

String replace

In java, we can replace any string character using .replace() method.

Let’s see what the usage of it is. There have two things – target element and replace by element.

public class strReplace {
    public static void main(String[] args) {
        String n=”snake”;
System.out.println(n);
        n=n.replace(“n”,”m”);
System.out.println(n);
    }
}
output

If you want to learn java string in depth:
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html