Java StringBuffer insert()

Java StringBuffer insert()

StringBuffer.insert()方法在StringBuffer中的指定位置插入指定数据类型的字符串表示。

语法

 str.insert(int position,  char x);
 str.insert(int position,  boolean x);
 str.insert(int position,  char[] x);
 str.insert(int position, float x);
 str.insert(int position, double x);
 str.insert(int position, long x);
 str.insert(int position, int x);

position是我们需要插入的字符串中的索引。中的索引,我们需要插入。

返回

该方法返回该对象的一个引用。

异常

position参数必须大于 大于或等于0,并且小于 或等于这个字符串的长度。

布尔输入

// Java program to demonstrate StringBuffer insert
// for boolean input.  
import java.lang.*;
  
public class GFG {
    public static void main(String[] args)
    {
        StringBuffer str = 
             new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
  
        // insert boolean value at offset 8
        str.insert(8, true);
  
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
    }
}

输出:

string = geeks for geeks
After insertion = geeks fotruer geeks

字符输入

// Java program to demonstrate StringBuffer insert
// for char input.  
import java.lang.*;
  
public class GFG {
    public static void main(String[] args)
    {
        StringBuffer str = 
           new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
  
        // insert boolean value at offset 8
        str.insert(8, 'p');
  
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
    }
}

输出:

string = geeks for geeks
After insertion = geeks fopr geeks

字符阵列输入

// Java program to demonstrate StringBuffer insert
// for char array input.  
import java.lang.*;
  
public class GFG {
    public static void main(String[] args)
    {
        StringBuffer str = 
               new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
  
        // character array to be inserted
        char cArr[] = { 'p', 'a', 'w', 'a', 'n' };
  
        // insert character array at offset 9
        str.insert(8, cArr);
  
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
    }
}

输出:

string = geeks for geeks
After insertion = geeks fopawanr geeks

浮动输入

// Java program to demonstrate StringBuffer insert
// for float input.
import java.lang.*;
  
public class GFG 
{
    public static void main(String[] args)
    {
        StringBuffer str =
            new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
          
        // insert float value at offset 3
        str.insert(8, 41.35f);
          
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
   }      
}

输出:

string = geeks for geeks
After insertion = geeks fo41.35r geeks

双重输入

// Java program to demonstrate StringBuffer insert
// for double input.
import java.lang.*;
  
public class GFG 
{
    public static void main(String[] args)
    {
        StringBuffer str = 
           new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
          
        // insert float value at offset 3
        str.insert(8, 41.35d);
          
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
   }      
}

输出:

string = geeks for geeks
After insertion = geeks fo41.35r geeks

长输入

// Java program to demonstrate StringBuffer insert
// for Long input.
import java.lang.*;
  
public class GFG 
{
    public static void main(String[] args)
    {
        StringBuffer str = new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
          
        // insert float value at offset 3
        str.insert(8, 546986L);
          
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
   }      
}

输出:

string = geeks for geeks
After insertion = geeks fo546986r geeks

输入

// Java program to demonstrate StringBuffer insert
// for Int input.
import java.lang.*;
  
public class GFG 
{
    public static void main(String[] args)
    {
        StringBuffer str = 
            new StringBuffer("geeks for geeks");
        System.out.println("string = " + str);
          
        // insert float value at offset 8
        int x = 10;
        str.insert(8, x);
          
        // prints stringbuffer after insertion
        System.out.print("After insertion = ");
        System.out.println(str.toString());
   }      
}

输出:

string = geeks for geeks
After insertion = geeks fo10r geeks

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程