📜  Java中的子字符串

📅  最后修改于: 2022-05-13 01:54:45.826000             🧑  作者: Mango

Java中的子字符串

substring() 方法有两种变体。本文描述了所有这些,如下所示:

1. String substring():这个方法有两种变体,返回一个新的字符串,它是这个字符串的子字符串。子字符串以指定索引处的字符开始并延伸到该字符串的结尾。子字符串的索引从 1 开始,而不是从 0 开始。

Syntax : 
public String substring(int begIndex)
Parameters : 
begIndex : the begin index, inclusive.
Return Value : 
The specified substring.
Java
// 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));
    }
}


Java
// 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));
    }
}


Java
// 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));
    }
}


输出:

The extracted substring is :  geeksforgeeks

2. String substring(begIndex, endIndex):这个方法有两个变种,返回一个新的字符串,它是这个字符串的一个子字符串。子字符串以指定索引处的字符开始,并延伸到该字符串的末尾或直到endIndex – 如果给定第二个参数,则为 1

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

// 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));
    }
}

输出:

The extracted substring  is :  geeks

可能的应用:子串提取在许多应用中都有使用,包括前缀和后缀提取。例如,名称中提取姓氏或从包含金额和货币符号的字符串中仅提取面额。后一种解释如下。

Java

// 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));
    }
}

输出 :

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