Saturday, December 11, 2010

Substring in Java

Substring is one of the most commonly used methods of the String class. Is is used for extracting a portion of the original string. This method is very useful when we are dealing with file names and extensions and want to extract some part of the Staring value.

Understanding substring()
For a good understanding of substring(), it is essential that we first see how Java stores the String in memory. For example if we say that:-
String str = "substring() is a method";

then in the memory the above String is stored as:-


from the above diagram it is clear that the indexing of Strings always start from zero, and goes till one less then the String length.

Substring uses this information to retrieve a part of the original string by providing the (starting) or (starting,ending) index values. The numbers mentioned in the above diagram are the index values for the characters of that String.

Syntax of substring() in Java
Substring comes in two flavor in Java. The two syntax of the method are:-

public String substring(int beginIndex)
Used to extract a portion of the String. The substring returned by this method starts from the character specified by the beginIndex value, and it ends till the last character of the original String.

public String substring(int beginIndex, int endIndex)
This method extracts a portion of the String. The substring returned by this method starts from the character specified by the beginIndex value and ends at the character specified by the endIndex value.

Exceptions for substring()
In case the starting Index value or the end Index value in the substring() method is out of the range of the String length, then the Java Virtual Machine throws a IndexOutOfBoundsException.

This exception shows that the Index value provided for the substring() method is either negative or larger than the String length.

substring() Examples
Example 1


    package JavaTest1;

    public class StringTest {

        public static void main(String[] args) {

            String str = "substring() is a method";
            String subStr;

            subStr = str.substring(5);
            System.out.println(subStr);
        }
    }

When the above code is executed, the output is:-
    ring() is a method

The below diagram explains the output received after the substring() operation.


Example 2

    package JavaTest1;

    public class StringTest {

        public static void main(String[] args) {

            String str = "substring() is a method";
            String subStrBeginEnd;

            subStrBeginEnd = str.substring(5, 21);
            System.out.println(subStrBeginEnd);
        }
    }

On execution of the above code, the output is:-
    ring() is a meth

The substring example is further explained by the diagram below.



Note:-The above diagram clears that the substring() method includes the starting index, but excludes the end index.

Do raise any queries if you need explanation on any part.

Related Readings
Java Substring Examples
Java Substring Index 
Substring Index Out of Range Exception

Related Articles
Convert int to String in Java
Convert int to String using Wrapper in Java
Java String Length Program
Reverse a String in Java

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.