Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programming

String str = "Hello, World!";
int index = str.indexOf("World");
System.out.println(index); // 7

In this example, we create a string str that contains the text “Hello, World!”. We then use the indexOf() method to search for the substring “World” in the string. The method returns the index of the first occurrence of the substring, which is 7 in this case. Finally, we print the index using the println() method.

If the specified substring is not found in the string, the indexOf() method returns -1. For example:

String str = "Hello, World!";
int index = str.indexOf("Java");
System.out.println(index); // -1

In this example, we try to search for the substring “Java” in the string “Hello, World!”. Since the substring is not found in the string, the method returns -1.

It’s important to note that the indexOf() method is case-sensitive, meaning that it will only find exact matches of the specified substring. If you want to perform a case-insensitive search, you can use the toLowerCase() or toUpperCase() methods to convert both the string and the substring to the same case before calling indexOf().

String str = "Hello, World!";
int index = str.lastIndexOf("o");
System.out.println(index); // 8

In this example, we create a string str that contains the text “Hello, World!”. We then use the lastIndexOf() method to search for the substring “o” in the string. The method returns the index of the last occurrence of the substring, which is 8 in this case. Finally, we print the index using the println() method.

If the specified substring is not found in the string, the lastIndexOf() method returns -1. For example:

String str = "Hello, World!";
int index = str.lastIndexOf("Java");
System.out.println(index); // -1

In this example, we try to search for the substring “Java” in the string “Hello, World!”. Since the substring is not found in the string, the method returns -1.

Like the indexOf() method, the lastIndexOf() method is also case-sensitive. If you want to perform a case-insensitive search, you can convert both the string and the substring to the same case before calling lastIndexOf().

String str = "Hello, World!";
boolean startsWithHello = str.startsWith("Hello");
boolean startsWithWorld = str.startsWith("World");
System.out.println(startsWithHello); // true
System.out.println(startsWithWorld); // false

In this example, we create a string str that contains the text “Hello, World!”. We then use the startsWith() method to check whether the string starts with the prefixes “Hello” and “World”. The first call to startsWith() returns true because the string does start with the prefix “Hello”, while the second call returns false because the string does not start with the prefix “World”.

Here’s an example of using the endsWith() method:

String str = "Hello, World!";
boolean endsWithWorld = str.endsWith("World!");
boolean endsWithJava = str.endsWith("Java");
System.out.println(endsWithWorld); // true
System.out.println(endsWithJava); // false

In this example, we use the endsWith() method to check whether the string ends with the suffixes “World!” and “Java”. The first call to endsWith() returns true because the string does end with the suffix “World!”, while the second call returns false because the string does not end with the suffix “Java”.

Both methods are case-sensitive, meaning that they will only return true if the prefix or suffix matches the string exactly, including the case of the characters. If you want to perform a case-insensitive check, you can convert both the string and the prefix or suffix to the same case before calling startsWith() or endsWith().

String str = "hello world";
String newStr = str.replace('o', 'e');
System.out.println(newStr); // "helle werld"

In this example, we create a string str containing the text “hello world”. We then call the replace() method on the string, specifying that we want to replace all occurrences of the character ‘o’ with the character ‘e’. The resulting string is “helle werld”.

It is also possible to use the replace() method to replace a substring with another substring. Here is an example:

String str = "The quick brown fox jumps over the lazy dog";
String newStr = str.replace("fox", "cat");
System.out.println(newStr); // "The quick brown cat jumps over the lazy dog"

In this example, we replace the substring “fox” with the substring “cat”.

The replaceAll() method is similar to replace(), but it uses regular expressions to specify the character or substring to be replaced. This makes it more powerful than replace(), but also more complex to use. Here is an example of using replaceAll() to replace all occurrences of the string “cat” with the string “dog”:

String str = "The quick brown cat jumps over the lazy cat";
String newStr = str.replaceAll("cat", "dog");
System.out.println(newStr); // "The quick brown dog jumps over the lazy dog"

In general, you should use replace() when you want to replace a fixed character or substring, and replaceAll() when you want to replace a pattern that can be specified using a regular expression.

String sentence = "I like apple pie, apple juice, and apple sauce.";
String newSentence = sentence.replaceFirst("apple", "banana");
System.out.println(newSentence);

In this example, we create a string sentence containing a sentence with multiple occurrences of the word “apple”. We then call the replaceFirst() method on the string, specifying that we want to replace the first occurrence of the word “apple” with the word “banana”. The resulting string is:

I like banana pie, apple juice, and apple sauce.

Note that only the first occurrence of the word “apple” is replaced with “banana”. The other occurrences of the word “apple” in the sentence remain unchanged.

It’s important to keep in mind that replaceFirst() method replaces only the first occurrence of the pattern in the string. If you want to replace all occurrences of the pattern, you should use the replaceAll() method instead.

String str = "  Hello, world!  ";
String trimmedStr = str.trim();
System.out.println(trimmedStr);

In this example, we create a string str that contains leading and trailing whitespaces. We then call the trim() method on the string, which returns a new string with the leading and trailing whitespaces removed. The resulting string is:

Hello, world!

Note that the original string str is unchanged by the trim() method. Also, if the string doesn’t have any leading or trailing whitespaces, the trim() method returns the original string.

If you want to remove only leading or trailing whitespaces, you can use the stripLeading() or stripTrailing() methods, respectively, introduced in Java 11.

String str = "  Hello, world!  ";
String strippedLeading = str.stripLeading();
String strippedTrailing = str.stripTrailing();
System.out.println(strippedLeading);
System.out.println(strippedTrailing);

This will output:

Hello, world!  
  Hello, world!

As you can see, stripLeading() removes leading whitespaces while stripTrailing() removes trailing whitespaces. If you want to remove both leading and trailing whitespaces, use strip() method introduced in Java 11.

String str = "  Hello, world!  ";
String stripped = str.strip();
System.out.println(stripped);

This will output:

Hello, world!

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories