Export (0) Print
Expand All
Expand Minimize

java.lang.String.substring(int offset, int endIndex)

Visual Studio 2005

Creates a new String object containing the contents of the existing String between two indices.


public String substring(int offset, int endIndex)

Parameters

Parameter Description

offset

The index of the first character within the current String to copy into the new java.lang.String object. An IndexOutofBoundsException is thrown if this index is invalid.

endIndex

The index of the last character within the current String to copy into the new java.lang.String object. An IndexOutofBoundsException is thrown if this index is invalid.

A new java.lang.String object containing the contents of the existing String between offset and endIndex.

// string_substring2.jsl

public class Program
{
    public static void main(String[] args)
    {
        String str = "Navy blue is my favorite color.";

        // Get a substring of the above string starting from
        // index 5 and ending at index 24.
        String newStr = str.substring(5, 24);

        // Display the two strings for comparison.
        System.out.println("old = " + str);
        System.out.println("new = " + newStr);
    }
}

/*
Output:
old = Navy blue is my favorite color.
new = blue is my favorite
*/

Community Additions

ADD
Show:
© 2014 Microsoft