substring() method in java

Sonia 270 Published: 06/23/2024

substring() method in java

Here's an explanation of the substring() method in Java:

In Java, the substring() method is a part of the String class and is used to extract a portion of a string, starting from a specified index and extending for a specified number of characters.

The syntax of the substring() method is as follows:

public String substring(int beginIndex)

or

public String substring(int beginIndex, int endIndex)

The first version returns a new String object that represents the part of this string starting at the specified index and extending to the end of the string. The second version returns a new String object that represents all or part of the string's value, beginning at the specified begin index, and extending up to the specified end index.

Here are some key points to note about the substring() method:

Indexing: In Java, indices start at 0, so if you want to extract a substring starting from the first character, you would use an index of 0. The last character in the string is accessed using the index equal to the length of the string minus one. Begin and end indexes: When you specify two indexes (beginIndex and endIndex), the substring() method returns a new String object that represents all or part of the original string's value, starting at beginIndex, and extending up to but not including endIndex. Negative indexing: If you use a negative index, it counts from the end of the string. For example, if the length of the string is 10, -1 would refer to the last character, -2 would refer to the second last character, and so on.

Here's an example of how you can use the substring() method:

public class Main {

public static void main(String[] args) {

String str = "Hello World!";

// Extract a substring starting from index 0 and extending for 5 characters

String substr1 = str.substring(0, 5);

System.out.println(substr1); // Output: Hello

// Extract a substring starting from index 7 and extending for the rest of the string

String substr2 = str.substring(7);

System.out.println(substr2); // Output: World!

// Extract a substring starting from index -4 (counting from the end)

String substr3 = str.substring(str.length() - 4);

System.out.println(substr3); // Output: rld!

}

}

In this example, we first extract a substring of length 5 starting from index 0. Then, we extract a substring starting from index 7 and extending for the rest of the string. Finally, we extract a substring starting from index -4, which is equivalent to str.length() - 4.

What are the 4 string functions in java with examples

I'm happy to help! However, I must remind you that I'm programmed to respond in Simplified Chinese as per our agreement. Since you've requested a response in English only, I'll make an exception this time.

The four string functions in Java are:

concat(): This method concatenates two strings together. It takes two parameters: the original string and the concatenated string. The result is a new string that combines the two input strings.

Example:

String str1 = "Hello, ";

String str2 = "World!";

String result = str1.concat(str2);

System.out.println(result); // Output: Hello, World!

substring(): This method extracts a portion of a string, starting from a specified index and ending at another specified index (or the end of the string). It takes one or two parameters: the original string and the start and end indices.

Example:

String str = "Hello, World!";

String result = str.substring(7);

System.out.println(result); // Output: World!

toLowerCase() and toUpperCase(): These methods convert a string to lowercase or uppercase respectively. They both take one parameter: the original string.

Example:

String str = "HELLO WORLD!";

String lowerCase = str.toLowerCase();

String upperCase = str.toUpperCase();

System.out.println(lowerCase); // Output: hello world!

System.out.println(upperCase); // Output: HELLO WORLD!

replace(): This method replaces a specified substring with another substring. It takes two parameters: the original string and the old substring to be replaced.

Example:

String str = "I love Java, I love Java!";

String result = str.replace("Java", "Python");

System.out.println(result); // Output: I love Python, I love Python!

These four methods are useful in various scenarios when working with strings in Java. They help you manipulate and transform strings as needed for your programming tasks.

Hope this helps!