Java Strings with Example -

Java Strings with Example

A string is a sequence of characters used to represent text in programming. In Java, strings are objects, and they make it easy to work with text. Let’s explore how strings function in Java and some useful methods to manipulate them.


Creating Strings

You can create a string by enclosing text in double quotes ("). You store this text in a variable.

Example:

String greeting = "Hello, World!";
System.out.println(greeting);  // Output: Hello, World!

In this example, "Hello, World!" is assigned to the variable greeting.


Important String Methods

Java provides several built-in methods to perform operations on strings. Here are some commonly used methods:

1. length(): Find the Length of a String

The length() method returns the number of characters in a string.

Example:

String message = "Learning Java!";
int length = message.length();
System.out.println("Length: " + length);  // Output: 15

In this case, length() counts the characters in "Learning Java!", which is 15.


2. concat(): Combine Strings

You can join two or more strings using the concat() method.

Example:

String firstName = "Alice";
String lastName = "Smith";

String fullName = firstName.concat(" ").concat(lastName);
System.out.println(fullName);  // Output: Alice Smith

Here, concat() joins firstName and lastName with a space in between.


3. toUpperCase() and toLowerCase(): Change Case

  • toUpperCase(): Converts a string to uppercase.
  • toLowerCase(): Converts a string to lowercase.

Example:

String text = "Java Programming";

System.out.println(text.toUpperCase());  // Output: JAVA PROGRAMMING
System.out.println(text.toLowerCase());  // Output: java programming

These methods help change the case of the entire string.


4. equals(): Compare Strings

The equals() method compares two strings and returns true if they are identical.

Example:

String str1 = "Hello";
String str2 = "hello";

if (str1.equals(str2)) {
    System.out.println("Strings are equal");
} else {
    System.out.println("Strings are not equal");  // Output: Strings are not equal
}

This comparison is case-sensitive, so "Hello" and "hello" are considered different.


5. substring(): Extract Part of a String

You can extract a portion of a string using the substring() method.

Example:

String sentence = "Welcome to Java Programming";
String part = sentence.substring(11, 15);  // Extract characters from index 11 to 14
System.out.println(part);  // Output: Java

In this example, substring(11, 15) extracts "Java" from the sentence.


6. replace(): Change Text in a String

The replace() method allows you to replace part of a string with new text.

Example:

String original = "Good Morning";
String updated = original.replace("Morning", "Evening");

System.out.println(updated);  // Output: Good Evening

Here, replace() changes "Morning" to "Evening" in the original string.


Concatenation Using +

You can also join strings using the + operator.

Example:

String name = "Bob";
String greeting = "Hello, " + name + "!";

System.out.println(greeting);  // Output: Hello, Bob!

In this case, the + operator adds the name variable to the greeting.


Real-Life Example: Creating a Message

Imagine you want to create a message for a user:

String userName = "John";
String message = "Hi " + userName + ",\nWelcome to our platform!";

System.out.println(message);

Output:

Hi John,
Welcome to our platform!

This example uses string concatenation to generate a custom message for the user.


Conclusion

Strings in Java are versatile for handling text. Here’s a quick recap:

  • String: A sequence of characters enclosed in quotes.
  • Common String Methods:
  • length(): Finds the length of a string.
  • concat(): Combines strings.
  • toUpperCase() / toLowerCase(): Changes the case of text.
  • equals(): Compares strings for equality.
  • substring(): Extracts part of a string.
  • replace(): Changes text in a string.

By understanding these basic string operations, you’ll be able to manage text effectively in your Java projects!

Leave a Comment