JAVA

Java Strings


What is String?

String is an object that represents a sequence of characters. The String class, which is part of the java.lang package, is used to create and manipulate strings.

Note:- Java strings contain alpha numeric values enclosed in double quotation.

Example

String firstName = "LearnCoding2k18";

 

There are two ways to create string object.

 

1. String Literal:
The most common way to create a string is by using string literals. You can create a string by enclosing a sequence of characters in double quotes.

Syntax

String str = "Hello, World!";

 

String Methods:

Java provides various methods for manipulating strings. 

length(): Returns the length of the string.
charAt(index): Returns the character at the specified index.
substring(startIndex, endIndex): Returns a substring from start index to end index (exclusive).
toUpperCase(): Converts the string to uppercase.
toLowerCase(): Converts the string to lowercase.
equals(otherString): Compares two strings for equality.

 

Example

 

public class StringMethodsExample {
    public static void main(String[] args) {
        // String Literal Creation
        String str = "Hello, World!";

        // Length of the String
        int length = str.length();
        System.out.println("Length of the string: " + length);

        // Character at a specific index
        char firstChar = str.charAt(0);
        System.out.println("First character: " + firstChar);

        // Substring
        String subString = str.substring(7); // Starting from index 7 to the end
        System.out.println("Substring from index 7: " + subString);

        // Uppercase and Lowercase
        String upperCaseStr = str.toUpperCase();
        String lowerCaseStr = str.toLowerCase();
        System.out.println("Uppercase: " + upperCaseStr);
        System.out.println("Lowercase: " + lowerCaseStr);

        // String Concatenation
        String newString = str + " Have a great day!";
        System.out.println("Concatenated String: " + newString);

        // Equality Comparison
        String anotherString = "hello, world!";
        boolean isEqual = str.equals(anotherString);
        System.out.println("String equality: " + isEqual);

        // Case-Insensitive Comparison
        boolean isEqualIgnoreCase = str.equalsIgnoreCase(anotherString);
        System.out.println("Case-insensitive equality: " + isEqualIgnoreCase);
    }
}

 

String Concatenation

Concatenation: Concatenation is the process of combining or linking together two or more strings to create a new string.

Example ( using method )

public class ConcatenationExample {
    public static void main(String[] args) {
        // Two strings to be concatenated
        String str1 = "Hello, ";
        String str2 = "World!";

        // Concatenate using the concat() method
        String result = str1.concat(str2);

        // Display the result
        System.out.println("Concatenated String: " + result);
    }
}

 

Example (Without method)

Strings in Java can be concatenated using the + operator as well.
 

String firstName = "Learn";
String lastName = "Coding";
String fullName = firstName + " " + lastName; // Results in "Learn Coding"

 

2. Using new Keyword:

We can also create a string using the new keyword and the String constructor.

Syntax

String str = new String("Learn Coding");

 

Example

public class CreateStringExample {
    public static void main(String[] args) {
    
        // Creating a string using new keyword
        String str = new String("Hello, Java!");

        // Displaying the created string
        System.out.println("Created String: " + str);
    }
}