Home  >  Article  >  Java  >  Use java's StringBuilder.insert() function to insert a string at the specified position

Use java's StringBuilder.insert() function to insert a string at the specified position

WBOY
WBOYOriginal
2023-07-24 21:37:482047browse

Use java's StringBuilder.insert() function to insert a string at a specified position

StringBuilder is a class used to handle variable strings in Java. It provides a variety of methods to operate strings, among which The insert() function is one of the common methods used to insert a string at a specified position. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples.

The insert() function is defined as follows:

public StringBuilder insert(int index, String str)

Among them, the index parameter indicates the position where the string is to be inserted, and the str parameter indicates the string to be inserted.

The following is a sample code that uses the insert() function to insert a string at a specified position:

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello World!");
        
        // 在指定位置插入字符串
        sb.insert(6, "Java ");
        
        // 输出结果
        System.out.println(sb.toString());
    }
}

Run the above code, the output result is:

Hello Java World!

You can see So, we successfully inserted the string "Java" into the 6th position of the original string "Hello World!" and got the new string "Hello Java World!".

In addition to inserting a string at a specified position, the insert() function can also insert a string at any position in the string. The following is a sample code that inserts a string at any position in the string:

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello World!");
        
        // 在字符串的任意位置插入字符串
        sb.insert(sb.indexOf("World"), "Java ");
        
        // 输出结果
        System.out.println(sb.toString());
    }
}

Run the above code, the output result is:

Hello Java World!

As you can see, we use the indexOf() function Locate the position of the string "World" in the original string, insert the string "Java" at that position, and get the new string "Hello Java World!".

In short, using java's StringBuilder.insert() function can easily insert a string at a specified position or any position. Through the above example code, I believe that readers have mastered the basic method of using the insert() function and can flexibly apply it in actual development.

The above is the detailed content of Use java's StringBuilder.insert() function to insert a string at the specified position. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn