Let's explore some important String Methods in Java. By leveraging these concepts and methods, you can write optimized, reliable, and maintainable code when working with strings.
contains()
The Java contains()
method searches for a sequence of characters in a string. It returns true
if the sequence exists; otherwise, it returns false
.
Syntax:
string_name.contains("sequence");
Returns: true
if the sequence exists, otherwise false
. Throws: NullPointerException
if the sequence is null
.
endsWith()
The Java endsWith()
method checks if a string ends with the given suffix.
Syntax:
string_name.endsWith("sequence or character");
Returns: true
or false
.
indexOf()
The Java indexOf()
method returns the index of a given character or substring. If not found, it returns -1
.
Common Variants:
int indexOf(int ch)
: Returns index of given character.int indexOf(int ch, int fromIndex)
: Returns index of given character starting fromfromIndex
.int indexOf(String substring)
: Returns index of given substring.int indexOf(String substring, int fromIndex)
: Returns index of given substring starting fromfromIndex
.
charAt()
The Java charAt()
method returns a character at the specified index.
Syntax:
char c = string_name.charAt(index);
Returns: Character at the specified index. Throws: StringIndexOutOfBoundsException
if index is out of range.
startsWith()
The Java startsWith()
method checks if a string starts with a given prefix.
Syntax:
public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int offset);
Returns: true
or false
.
substring()
The Java substring()
method returns a part of a string.
Syntax:
public String substring(int startIndex);
public String substring(int startIndex, int endIndex);
Returns: Extracted substring. Throws: StringIndexOutOfBoundsException
if indices are invalid.
toLowerCase()
The Java toLowerCase()
method converts all characters in a string to lowercase.
Syntax:
public String toLowerCase();
public String toLowerCase(Locale locale);
Returns: Lowercase string.
toUpperCase()
The Java toUpperCase()
method converts all characters in a string to uppercase.
Syntax:
public String toUpperCase();
public String toUpperCase(Locale locale);
Returns: Uppercase string.
Understanding ==
Operator
The ==
operator checks whether two string variables point to the same memory location. If two strings have the same content but are stored in different memory locations, ==
will return false.
Example:
String s1 = "java";
String s2 = "java";
String s3 = new String("java");
System.out.println(s1 == s2); // true (same reference from String pool)
System.out.println(s1 == s3); // false (different memory locations)
By understanding these concepts, you will be able to efficiently compare and work with strings in Java.
String Concatenation in Java
String concatenation in Java creates a new string by combining multiple strings. There are two ways to concatenate strings in Java:
Using
+
(string concatenation) operator:String s1 = "Hello"; String s2 = " World"; String s3 = s1 + s2; System.out.println(s3); // Output: Hello World
Using
concat()
method:String s1 = "Hello"; String s2 = " World"; String s3 = s1.concat(s2); System.out.println(s3); // Output: Hello World
By understanding these concepts, you will be able to efficiently compare, modify, and concatenate strings in Java.
Summary
Understanding string comparison methods like equals()
, compareTo()
, and ==
helps developers avoid common pitfalls related to reference and content comparison. Additionally, string concatenation and essential methods such as contains()
, startsWith()
, substring()
, and toUpperCase()
provide powerful ways to manipulate strings effectively.
By leveraging these concepts and methods, Java developers can write optimized, reliable, and maintainable code when working with strings.