Related Topics

Data Structure
String str1 = "Hello, World!";
Using the
new
keyword:
String str2 = new String("Hello, World!");
In both cases, str1
and str2
will contain the same string, “Hello, World!”. The first method is more commonly used because it is shorter and more readable.
Note that strings are immutable in Java, which means that once a string is created, it cannot be changed. If you need to modify a string, you will need to create a new string object.
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "John Doe"
String message = "Hello, " + fullName + "!"; // "Hello, John Doe!"
In the above example, the +
operator concatenates the firstName
and lastName
variables with a space in between, and then concatenates the resulting string with the "Hello, "
and "!"
strings.
You can also use the concat()
method to concatenate strings in Java:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName); // "John Doe"
In the above example, the concat()
method is called twice to concatenate the firstName
and lastName
variables with a space in between.
String str = "Hello, world!";
int length = str.length();
System.out.println("The length of the string is " + length);
Output:
The length of the string is 13
String str1 = "hello";
String str2 = "world";
if(str1.equals(str2)) {
System.out.println("The strings are equal");
} else {
System.out.println("The strings are not equal");
}
Using compareTo() method:
String str1 = "hello";
String str2 = "world";
int result = str1.compareTo(str2);
if(result == 0) {
System.out.println("The strings are equal");
} else if(result < 0) {
System.out.println("The first string is smaller");
} else {
System.out.println("The second string is smaller");
}
Note: When comparing strings in Java, it is important to remember that the equals()
method compares the actual string contents while the ==
operator compares the object references.
public static String reverseString(String str) {
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
Using recursion:
public static String reverseString(String str) {
if (str.isEmpty()) {
return str;
}
return reverseString(str.substring(1)) + str.charAt(0);
}
Using the
reverse()
method of a StringBuilder:
public static String reverseString(String str) {
return new StringBuilder(str).reverse().toString();
}
String str = "Hello, World!";
boolean containsSubstring = str.contains("World");
boolean containsChar = str.contains("o");
System.out.println(containsSubstring); // Output: true
System.out.println(containsChar); // Output: true
In the example above, the contains()
method is used to check if the string str
contains the substring “World” and the character “o”. The output shows that both checks return true
, indicating that the string contains the specified substring and character.
int indexOf(int ch)
int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex)
The first two variants of the indexOf()
method search for the index of the specified character, starting from the beginning of the string or from the specified index.
The last two variants of the indexOf()
method search for the index of the specified substring, starting from the beginning of the string or from the specified index.
Here’s an example:
String str = "Hello, world!";
int index1 = str.indexOf('o'); // returns 4
int index2 = str.indexOf("world"); // returns 7
int index3 = str.indexOf('o', 5); // returns 8
int index4 = str.indexOf("world", 5); // returns -1
In this example, the indexOf()
method is used to find the index of the letter “o” and the substring “world” in the string “Hello, world!”. The second and fourth variants of the indexOf()
method are used to search for the specified character or substring starting from the index 5.
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)
Here is an example:
String str = "Hello, world!";
String substr1 = str.substring(7); // substr1 = "world!"
String substr2 = str.substring(0, 5); // substr2 = "Hello"
In the first line of the example, the substring()
method is called with a single parameter, which is the starting index of the substring. This will extract the substring starting from the 7th character (“w”) to the end of the string.
In the second line of the example, the substring()
method is called with two parameters: the starting index and the ending index of the substring. This will extract the substring starting from the 0th character (“H”) up to, but not including, the 5th character (“,”).
String str = "apple,banana,orange";
String[] fruits = str.split(",");
for (String fruit : fruits) {
System.out.println(fruit);
}
In the example above, the split()
method is called on the str
string with the ,
delimiter. The method returns an array of strings fruits
, where each element of the array is a substring of str
that is delimited by the ,
character. The for
loop is then used to iterate over the elements of the fruits
array and print each element to the console.
The output of the above code snippet would be:
apple
banana
orange




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.