DEV Community

mannymodrono
mannymodrono

Posted on

String Class Java

Strings

Strings are variables used in Java for storing text. They are immutable which means they cannot be changed.

There are two different ways to create strings:

String literal
String s1= "John";

Using the new keyword
String s2 = new String("Tim");

Commonly Used String Methods

indexOf()- returns the position of the first occurrence of a specified string as an integer.
System.out.println(s1.indexOf("o"));
1

length()-returns the length of the string as an integer.
System.out.println(s2.length());
3

substring()-returns characters from a string from a specified beginning to a specified end point.
System.out.println(s1.substring(1,3));
oh

toLowerCase()/toUpperCase()- converts the string to uppercase or lowercase.
System.out.println(s1.toUpperCase());
JOHN

References

https://www.geeksforgeeks.org/strings-in-java/

https://www.w3schools.com/java/java_strings.asp

https://www.w3schools.com/java/java_ref_string.asp

Top comments (0)