The JavaScript String substring() function is used to extract a portion of a string and return it as a new string. It takes two parameters: the starting index and the ending index of the substring. The starting index is inclusive, meaning the character at that index is included in the substring, while the ending index is exclusive, meaning the character at that index is not included in the substring. If the ending index is not specified, the substring will include all characters from the starting index to the end of the string. If the starting index is greater than the ending index, the substring function will swap the two values before extracting the substring. Keep reading below to learn how to Javascript String substring in Java.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Javascript String substring in Java With Example Code

JavaScript String substring() method is used to extract a part of a string and return it as a new string. In Java, we can use the substring() method of the String class to achieve the same functionality.

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

String substring(int beginIndex)

String substring(int beginIndex, int endIndex)

The first parameter specifies the starting index of the substring, and the second parameter specifies the ending index of the substring. If the second parameter is not specified, the substring will include all characters from the starting index to the end of the string.

Here is an example of using the substring() method in Java:

String str = "Hello World";
String substr1 = str.substring(0, 5);
String substr2 = str.substring(6);
System.out.println(substr1); // Output: "Hello"
System.out.println(substr2); // Output: "World"

In the above example, we first create a string “Hello World”. We then use the substring() method to extract the substring “Hello” by specifying the starting index as 0 and the ending index as 5. We also extract the substring “World” by specifying the starting index as 6, which is the index of the first character of “World”, and not specifying the ending index.

In conclusion, the substring() method in Java is a useful tool for extracting substrings from strings. It is similar to the JavaScript String substring() method and can be used in a similar way.

Equivalent of Javascript String substring in Java

In conclusion, the Java String class provides a substring() method that is equivalent to the JavaScript substring() function. Both methods allow you to extract a portion of a string based on a starting index and an optional ending index. However, there are some differences in the way these methods handle negative indices and out-of-bounds indexes. It’s important to understand these differences when working with strings in Java or JavaScript. By using the substring() method in Java, you can easily manipulate strings and extract the data you need for your application.

Contact Us