The String
is a collection of characters that will be enclosed by " "
.
Mainly the strings help to store the text information
.
//syntax for java strings
String name = "Smart Cookie";
Here the name
is a variable
which is of string type
that contains the value smart cookie
The string
in java is nothing but an object
that contains the methods which will help to perform some operations on strings.
length()
toLowerCase()
.toUpperCase()
indexOf()
length()
Method :
In strings
we have a method called length()
which will count the length of the string.
public class Main{
public static void main(String[] args){
String name = "SmartCookie is a coding learning platform";
System.out.println("String length : " + name.length());
}
}
toLowerCase()
Method:
toLowerCase() method is helpful to convert the string into all lowercase
characters.
public class Main{
public static void main(String[] args){
String name = "SmartCookie Is a Coding Learning Platform";
System.out.println("Lower Case : " + name.toLowerCase());
}
}
Output:
toUpperCase()
Method:
toUpperCase() method is helpful to convert the string into all UpperCase
Characters.
public class Main{
public static void main(String[] args){
String name = "SmartCookie Is a Coding Learning Platform";
System.out.println("Upper Case : " + name.toUpperCase());
}
}
Output:
indexOf()
Method:
To find a specific character
in a string, we are using one method in java which is indexOf()
which will return the index of the first occurrence of the specified text in a string.
public class Main{
public static void main(String[] args){
String name = "SmartCookie Is a Coding Learning Platform";
System.out.println("Position is = "+ name.indexOf("Coding"));
}
}
Output: