SoFunction
Updated on 2025-04-05

Java String Intercept and Common Scenarios and Methods Detailed Explanation

Detailed explanation of Java string interception: Common scenarios and methods

In Java development, intercepting strings is a very common operation, whether it is to get file names or extract certain specific content. This article introduces in detail various methods of intercepting the last bit of a string and other common intercepting operations to help developers get started quickly.

1. Intercept the last bit of the string

1.1 Intercept with substring()

substring()It is the most commonly used string interception method, and supports obtaining partial strings through index positions. The theoretical format is as follows:

String result = (("\\") + 1);

Notice: When special characters are involved (such as backslashes\) is required\\Escape.

Example:

public class Test {
    public static void main(String[] args) {
        String fileName = "E:/eclipse_workspace1/FtpOperateFile/logs/";
        String newFileName = (("/") + 1);
        ("The last file name is=====>" + newFileName);
    }
}

Output result:
The last file name is =====>

1.2 Use split() to intercept

split()The method can cut the string according to the specified delimiter and return an array of strings.

Notice: When using special characters (such as/or.) as a separator, you need to use\\Escape.

Example:

public class Test {
    public static void main(String[] args) {
        String fileName = "E:/eclipse_workspace1/FtpOperateFile/logs/";
        // Use / for cutting        String[] splitFileName = ("\\/");
        // Get the last file name        String lastName = splitFileName[ - 1];
        ("The last file name is=====>" + lastName);
    }
}

Output result:
The last file name is =====>

1.3 Special handling of directories under Windows

On Windows systems, the path separator is usually a backslash\, need to pay attention to escaping. Here is an example:

public class Test {
    public static void main(String[] args) {
        String filePath = "C:\\Program Files\\Java\\jdk-17\\bin\\";
        String fileName = (("\\") + 1);
        ("The file name is=====>" + fileName);
    }
}

Output result:
The file name is =====>

1.4 Special handling of directories under Linux

The path separator in Linux system is a forward slash/, so it is relatively simple to handle. For example:

public class Test {
    public static void main(String[] args) {
        String filePath = "/usr/local/bin/java";
        String fileName = (("/") + 1);
        ("The file name is=====>" + fileName);
    }
}

Output result:
The file name is =====>java

2. Common ways to intercept strings

2.1 Common methods

Java provides the following two common string interception methods:

  • split(): Split the string according to the separator and return the string array.
  • substring(): Intercept strings based on index.

2.2 Method introduction and application examples

Method 1:split()

1. Use one parameter:split(String regex)

Splitting the string according to the given regular expression. Note that regular expressions may cause performance losses.

Example:

String str = "AB@CD";
String[] parts = ("@");
for (String part : parts) {
    (part);
}

Output result:

AB  
CD  

2. Use two parameters: split(String regex, int limit)

Split strings based on separator and number of splits.

Example:

String str = "HelloWorld@qq@com";
String[] parts = ("@", 2);
for (String part : parts) {
    (part);
}

Output result:

HelloWorld  
qq@com  

Method 2:substring()

1. Single parameter:substring(int beginIndex)

Start from the specified index and end of the string.

Example:

String sb = "HelloWorld@";
String result = (5);
(result);

Output result:
World@

2. Double parameters:substring(int beginIndex, int endIndex)

Intercept from the specified starting index to the end index (not including the characters corresponding to the end index).

Example:

String sb = "HelloWorld@";
String result = (0, 5);
(result);

Output result:
Hello

3. Intercept according to specific characters

Can be passedindexOf()Methods obtain the location of a specific character, thereby flexibly intercepting strings.

Example:

String sb = "HelloWorld@";
String result = (0, ("@"));
(result);

Output result:
HelloWorld

Summarize

method Applicable scenarios Return type Things to note
split() Splitting strings by specific characters or regular expressions String array Regular performance may be affected
substring() Intercept part of string or process path according to index String Be careful of indexing to cross boundaries

Different methods are suitable for different scenarios. For example:

  • When you need to split it simply, usesplit()More intuitive.
  • When processing paths or specific characters intercept,substring()More flexible and efficient.

By flexibly combining these methods, string processing problems in daily development can be quickly solved!

Here is the article about Java string interception: This is the end of this article about common scenarios and methods. For more related Java string interception content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!