String method avatar
a year ago
1.8 kB
// String Methods
   There are many string methods in java. Some of them are :
      :- charAt() ,  concat() ,length() ,compareTo(),contains(), split()
          indexOf() , toString() , replace()
import java.lang.String;
import java.lang.*;
class Main {
    public static void main(String[] args) {
        String str1 = "Hello world";
        String str2 = "Hello World";
        // charAt
        System.out.println(str1.charAt(2)); // charAt will give the character that is in that position
         System.out.println(str1.length()); // This will show the length of the string
         // compareTo()
         System.out.println(str1.compareTo(str2)); // compareTo() compare two string
         // concat()
         System.out.println(str1.concat(str2)); // concat() add two string
         System.out.println(str1.contains("Hello")); // contains() find a particuler string among the main string. if it finds then return true 
         System.out.println("Index of H is " + str1.indexOf("w")); // It will find the index of a character in that string
         //toString()  // This will return string representation of an object intance
         Integer obj = new Integer(10);
         String s1 = obj.toString();
         String s2 = obj.toString(80);
         System.out.println("The String representation is " + s1);
         System.out.println("\nThe String representation is " + s2);
         // replace() 
         String s11 = "Shot";
         String replace = s11.replace('o', 'u'); // replace the character