Your Course Progress

Topics
0 / 0
0.00%
Practice Tests
0 / 0
0.00%
Tests
0 / 0
0.00%
Assignments
0 / 0
0.00%
Content
0 / 0
0.00%
% Completed

Admission

Java String and Methods

This article provides a comprehensive overview of the Java String class and its various methods.

Strings are fundamental data types in Java used for storing and manipulating text. The Java String class provides a rich set of methods for working with strings, including string concatenation, searching, comparison, and more. This article will explore some of the key methods offered by the String class.

Java String and Methods

Do You Know?

In Java, strings are immutable, meaning their values cannot be changed after they are created. Instead, new String objects are created whenever you modify a string.

Strings are fundamental data types in Java used for storing and manipulating text. The Java String class provides a rich set of methods for working with strings, including string concatenation, searching, comparison, and more. This article will explore some of the key methods offered by the String class.

Table of Contents

  1. String and Methods

The Java String class is immutable, meaning its values cannot be modified directly. When you perform any operation that appears to change a string, a new String object is created with the modified content. Here's a look at some commonly used String methods:

Creating String Objects

String str1 = "Hello World"; // Using String literal
String str2 = new String("Hello World"); // Using constructor

Concatenation

To concatenate strings, use the + operator:

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // Result: John Doe

Length

To find the length of a string, use the length() method:

String message = "This is a message";
int length = message.length(); // length = 17

Character Access

To access a specific character in a string, use the charAt() method:

String text = "Hello";
char firstCharacter = text.charAt(0); // firstCharacter = 'H'

Substring

To extract a portion of a string, use the substring() method:

String sentence = "This is a sentence";
String part = sentence.substring(5, 10); // part = "is a"

The substring() method takes two arguments: the starting index (inclusive) and the ending index (exclusive).

Comparison

To compare strings, use the equals() method for case-sensitive comparison and the equalsIgnoreCase() method for case-insensitive comparison:

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

System.out.println(str1.equals(str2)); // false (case-sensitive)
System.out.println(str1.equalsIgnoreCase(str2)); // true (case-insensitive)
System.out.println(str1.equals(str3)); // true

Searching

To find the index of a specific character or substring, use the indexOf() method:

String text = "This is a test";
int index = text.indexOf("is"); // index = 2

The indexOf() method returns the first occurrence of the specified character or substring. If not found, it returns -1.

Replacing

To replace a character or substring with another, use the replace() method:

String text = "This is a test";
String newText = text.replace("test", "example"); // newText = "This is a example"
Important Note

The replace() method does not modify the original string. It creates a new string with the replaced characters or substring.

Trimming

To remove leading and trailing whitespace from a string, use the trim() method:

String text = "  Hello World  ";
String trimmedText = text.trim(); // trimmedText = "Hello World"

Case Conversion

To convert a string to uppercase or lowercase, use the toUpperCase() and toLowerCase() methods:

String text = "Hello World";
String uppercase = text.toUpperCase(); // uppercase = "HELLO WORLD"
String lowercase = text.toLowerCase(); // lowercase = "hello world"

Splitting

To split a string into an array of substrings based on a delimiter, use the split() method:

String text = "This,is,a,test";
String[] parts = text.split(",");

for (String part : parts) {
  System.out.println(part); // Outputs: This, is, a, test (each on a new line)
}

Formatting

The String class also offers methods for formatting strings, such as printf() and format(). These methods allow you to control the output of strings with specific formats, including padding, alignment, and data type conversions.

  • Java String objects are immutable.
  • The String class provides methods for string manipulation, including concatenation, length, character access, substring extraction, comparison, searching, replacing, trimming, case conversion, and splitting.
  • Methods like equals() and equalsIgnoreCase() are used for comparing strings.
  • The indexOf() method finds the first occurrence of a character or substring.
  • The replace() method creates a new string with replaced characters or substrings.
  • The trim() method removes leading and trailing whitespace.
  • The toUpperCase() and toLowerCase() methods convert strings to uppercase or lowercase.
  • The split() method divides a string into an array of substrings based on a delimiter.
  • Methods like printf() and format() are used for string formatting.

Discussion