Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-07-24 21:37:48
Original
2140 people have browsed it

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)
Copy after login

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());
    }
}
Copy after login

Run the above code, the output result is:

Hello Java World!
Copy after login
Copy after login

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());
    }
}
Copy after login

Run the above code, the output result is:

Hello Java World!
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template