Strings (Part 8)
Java Strings
Strings are used for storing text.
A String
variable contains a collection of characters surrounded by double quotes:
Some Methods of Strings:
Eg:-
class MyClass {
public static void main(String[] args) {
// create string using the new keyword
String example = new String("Hello! World");
// returns the substring World
System.out.println("Using the subString(): " + example.substring(7));
// converts the string to lowercase
System.out.println("Using the toLowerCase(): " + example.toLowerCase());
// converts the string to uppercase
System.out.println("Using the toUpperCase(): " + example.toUpperCase());
// replaces the character '!' with 'o'
System.out.println("Using the replace(): " + example.replace('!', 'o'));
}
}
Output:-
Using the subString(): World
Using the toLowerCase(): hello! world
Using the toUpperCase(): HELLO! WORLD
Using the replace(): Helloo World
Eg:-
class MyClass {
public static void main(String[] args) {
// create strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
// compare first and second strings
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
//compare first and third strings
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
}
Output:-
Strings first and second are equal: true
Strings first and third are equal: false
As Strings is a very enormous concept , these methods and information above will let you know the working of Strings . More complicated concepts of Strings would be added in the future tutorials.So stay tuned !