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
equals() string methodpublic 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)); } }
Let’s Run the code
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
equallgnore() string methodpublic 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)); } }
Let’s Run the code
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
compareTo() string methodpublic 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)); } }
Let’s Run the code
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
concat() string methodpublic 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); } }
Let’s Run the code
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
substring() string methodpublic 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)); } }
Let’s Run the code
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
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
valueOf()
This static function converts the other type value into string type.
Syntax
String.valueOf(value);
Input Value
Let’s Run the code
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
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
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
0 Comments