Existem duas variantes do método substring(). Este artigo descreve todos eles, da seguinte maneira: 

1. String substring(): Este método tem duas variantes e retorna uma nova string que é uma substring desta string. A substring começa com o caractere no índice especificado e se estende até o final desta string. E o índice de substring começa em 1 e não em 0.

Syntax : 
public String substring(int begIndex)
Parameters : 
begIndex : the begin index, inclusive.
Return Value : 
The specified substring.
// Java code to demonstrate the
// working of substring(int begIndex)
public class Substr1 {
    public static void main(String args[])
    {
 
        // Initializing String
        String Str = new String("Welcome to geeksforgeeks");
 
        // using substring() to extract substring
        // returns (whiteSpace)geeksforgeeks
       
        System.out.print("The extracted substring is : ");
        System.out.println(Str.substring(10));
    }
}

Saída: 

The extracted substring is :  geeksforgeeks

2. String substring (begIndex, endIndex): Este método tem duas variantes e retorna uma nova string que é uma substring desta string. A substring começa com o caractere no índice especificado e se estende até o final desta string ou até endIndex - 1 se o segundo argumento for fornecido. 

Syntax : 
public String substring(int begIndex, int endIndex)
Parameters : 
beginIndex :  the begin index, inclusive.
endIndex :  the end index, exclusive.
Return Value : 
The specified substring.
// Java code to demonstrate the
// working of substring(int begIndex, int endIndex)
public class Substr2 {
    public static void main(String args[])
    {
 
        // Initializing String
        String Str = new String("Welcome to geeksforgeeks");
 
        // using substring() to extract substring
        // returns geeks
        System.out.print("The extracted substring  is : ");
        System.out.println(Str.substring(10, 16));
    }
}

Saída: 

The extracted substring  is :  geeks

Aplicação possível: a extração de substring encontra seu uso em muitos aplicativos, incluindo extração de prefixo e sufixo. Por exemplo, para extrair um sobrenome a partir do nome ou extrair única denominação de uma string contendo tanto símbolo montante e moeda. O último é explicado a seguir.  

// Java code to demonstrate the
// application of substring()
public class Appli {
    public static void main(String args[])
    {
 
        // Initializing String
        String Str = new String("Rs 1000");
 
        // Printing original string
        System.out.print("The original string  is : ");
        System.out.println(Str);
 
        // using substring() to extract substring
        // returns 1000
        System.out.print("The extracted substring  is : ");
        System.out.println(Str.substring(3));
    }
}

Saída : 

The original string  is : Rs 1000
The extracted substring  is : 1000

Este artigo é uma contribuição de Astha Tyagi . Se você gosta de GeeksforGeeks e gostaria de contribuir, você também pode escrever um artigo usando write.geeksforgeeks.org ou enviar seu artigo para review-team@geeksforgeeks.org. Veja o seu artigo na página principal do GeeksforGeeks e ajude outros Geeks. 
Escreva comentários se encontrar algo incorreto ou se quiser compartilhar mais informações sobre o tópico discutido acima.