String is the class (pre-defined) provide by java for storing the string value. It means when we create an object of string class then we can store a string value as. It is immutable. String is a Group of characters is called string. If we store a string value using string class then the length of the string will be fix means no further amendment will happen in that string.

String Method in Java

equals()

This returns true if given both strings are identical.

Syntax

Obj.equals(obj2);

Example of equals() string method

Input Value

public class lowerf {
     
    public static void main(String args[]){  
      
    String a="1";  
    String b="2";  
    String c="1";  
    String d="2";  
    System.out.println(a.equals(b)); 
    System.out.println(a.equals(c)); 
    System.out.println(a.equals(d)); 
        } 
}
equals() string method

Let’s Run the code

Input Value

Output Value

Output Value

equalIgnoreCase()

This function compares two strings by ignoring case. If both are same returns true oterwise false.

Syntax

obj1.equalsIgnoreCase(obj2);

Example of equallgnore() string method

Input Value

public class lowerf {
     
    public static void main(String args[]){  
      
    String a="1";  
    String b="2";  
    String c="1";  
    String d="2";  
    System.out.println(a.equalsIgnoreCase(b)); 
    System.out.println(a.equalsIgnoreCase(c)); 
    System.out.println(a.equalsIgnoreCase(d)); 
        } 
}
equallgnore() string method

Let’s Run the code

Input Value

Output Value

Output Value

compareTo()

This method compares two string and returns an integer value as. It compares string on its ASCII value.

  • +ve = String 1 > String 2
  • -ve = String 1 < string 2
  • 0 = String 1 = String 2

Syntax

String1.compareTo(String2);

Example of compareTo() string method

Input Value

public class lowerf {
     
    public static void main(String args[]){  
        String a="hello";  
        String b="basic";  
        String c="engineer";  
        System.out.println(a.compareTo(b));  
        System.out.println(b.compareTo(c));
        } 
}
compareTo() string method

Let’s Run the code

Input Value

Output Value

Output Value

concat()

This function concat two strings and store it in third one.

Syntax

str3=str1.concat(str2);

Example of concat() string method

Input Value

public class lowerf {
     
    public static void main(String args[]){    
        String a="basic";    
        a.concat("engineer");    
        System.out.println(a);    
        a=a.concat(" basic enginner is a best learning ");    
        System.out.println(a);   
        } 
}
concat() string method

Let’s Run the code

Input Value

Output Value

Output Value

Substring()

string within string called substring. Tis method is used for finding the substring from string

Syntax

String2=String1.substring(start.pos);

Example of substring() string method

Input Value

public class lowerf {
     
    public static void main(String args[]){  
        String a="basic engineer";  
        System.out.println(a.substring(3,7)); 
        System.out.println(a.substring(8));  
        } 
}
substring() string method

Let’s Run the code

Input Value

Output Value

Output Value

indexOf()

This method returns the index position of the given character. It retruns the first occurrence index number of the character.

Syntax

obj.indexOf(‘char’);

Input Value

public class lowerf {
     
    public static void main(String args[]){  
        String a="hello student basic engineer";  
         
        int index1=a.indexOf("basic"); 
        int index2=a.indexOf("hello");  
        System.out.println(index1+"  "+index2);
          
        int index3=a.indexOf("student",4); 
        System.out.println(index3); 
      
        int index4=a.indexOf('s');
        System.out.println(index4); 
        } 
}

Let’s Run the code

Output Value

Output Value

tostring()

This static function converts the other type object value into string type

Syntax

Obj.tostring();

Input Value

public class lowerf {
     
    public static void main(String args[]){  
         Integer p=15;
         String s=p.toString();
         System.out.println(s);
        } 
}

Let’s Run the code

Output Value

Output Value

valueOf()

This static function converts the other type value into string type.

Syntax

String.valueOf(value);

Input Value

Let’s Run the code

Input Values

Output Value

Output Value

split()

This method splits this string against given regular expression and returns a char array.

Syntax

String split(String regex) 

Input Value

public class lowerf {
     
    public static void main(String args[]){  
        String a="basic engineer hello student";  
        String[] words=a.split("\\s"); 
  
        for(String b:words){  
        System.out.println(b);  
            }  
        } 
}

Let’s Run the code

Input Values

Output Value

Output Value

join()

This method returns a string joined with a given delimiter

Syntax

public static String join(CharSequence delimiter, CharSequence... elements)    

Input Value

public class lowerf {
     
    public static void main(String args[]){  
        String joinString1=String.join("-","basic","engineer","student","hello");  
        System.out.println(joinString1);  
        } 
}

Let’s Run the code

Input Values

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *