Java StringBuilder insert() 方法



Java StringBuilder insert() 方法用于将传递的参数字符串插入到 StringBuilder 对象中。

insert() 方法接受不同类型的参数,例如布尔值、整数、字符等。如果给定的偏移量无效,则会抛出异常。

insert() 方法具有12 个多态变体,具有不同的参数:布尔值、字符、字符数组、CharSequence、双精度浮点数、单精度浮点数、整数、长整数、对象、字符串和 StringBuilder。(以下是所有多态变体的语法)。

偏移量参数必须大于或等于 0,并且小于或等于此序列的长度。

语法

以下是Java StringBuilder insert() 方法的语法:

public StringBuilder insert(int offset, boolean b)// first syntax 
public StringBuilder insert(int offset, char c)// second syntax
public StringBuilder insert(int offset, char[] str)// third syntax
public StringBuilder insert(int index, char[] str, int offset, int len)// fourth syntax
public StringBuilder insert(int dstOffset, CharSequence s)// fifth syntax
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end)// sixth syntax
public StringBuilder insert(int offset, double d)// seven syntax
public StringBuilder insert(int offset, float f)// eight syntax
public StringBuilder insert(int offset, int i)// nine syntax
public StringBuilder insert(int offset, long l)// ten syntax
public StringBuilder insert(int offset, Object obj)// eleven syntax
public StringBuilder insert(int offset, String str)// twelve syntax 

参数

  • offset - 这是偏移量。

  • b - 这是布尔值。

  • c - 这是字符值。

  • strch - 这是字符数组。

  • index - 这是要插入子数组的位置。

  • len - 这是要插入的子数组中字符的数量。

  • dstoffset - 这是偏移量。

  • s - 这是要插入的序列。

  • start - 这是要插入的子序列的起始索引。

  • end - 这是要插入的子序列的结束索引。

  • d - 这是双精度浮点数的值。

  • f - 这是单精度浮点数的值。

  • i - 这是整数值。

  • l - 这是长整数值。

  • obj - 这是对象的值。

  • str - 这是字符串的值。

返回值

此方法返回对此对象的引用。

示例:将布尔值插入到当前序列

如果我们将布尔值作为参数传递给该方法,则 insert() 方法会将布尔值追加到当前序列。

在以下程序中,我们使用值“Hello”实例化StringBuilder 类。然后,使用insert() 方法,我们尝试在指定的偏移量 0处将布尔值“false”插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("Hello ");
      System.out.println("The given string is: " + sb);
      //initialize the offset and boolean values
      int offset = 0;
      boolean bool = false;
      System.out.println("The initialize offset and boolean values are: " + offset + " and " + bool);
      //using the insert() method
      System.out.println("After insert boolean value the string is: " + sb.insert(offset, bool));
   }
}

输出

执行上述程序后,将产生以下结果:

The given string is: Hello   
The initialize offset and boolean values are: 0 and false
After insert boolean value the string is: falseHello    

示例:将双精度浮点数插入到当前序列

如果我们将双精度浮点数作为参数传递给该方法,则此方法会将双精度浮点数插入到当前序列。

在以下示例中,我们使用值“double ”创建StringBuilder 类的对象。使用insert() 方法,我们尝试在指定的偏移量索引 6处将双精度浮点数“34.5d”插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("double");
      System.out.println("The given string is: " + sb);
      //initialize the offset and double values
      int offset = 6;
      double d = 34.5d;
      System.out.println("The initialize offset and double values are: " + offset + " and " + d);
      //using the insert() method
      System.out.println("After insert double value the string is: " + sb.insert(offset, d));
   }
}

输出

以下是上述程序的输出:

The given string is: double
The initialize offset and double values are: 6 and 34.5
After insert double value the string is: double34.5

示例:将整数插入到当前序列

如果我们将整数值作为参数传递给该方法,则 insert() 方法会将整数值插入到此序列。

在此程序中,我们创建了一个值为“Integer”的 StringBuilder。然后,使用insert() 方法,我们尝试在指定的偏移量索引 1处将整数值“10”插入到此序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("Integer");
      System.out.println("The given string is: " + sb);
      //initialize the offset and int values
      int offset = 1;
      int i = 10;
      System.out.println("The initialize offset and int values are: " + offset + " and " + i);
      //using the insert() method
      System.out.println("After insert int value the string is: " + sb.insert(offset, i));
   }
}

输出

上述程序产生以下输出:

The given string is: Integer
The initialize offset and int values are: 1 and 10
After insert int value the string is: I10nteger

示例:将单精度浮点数插入到当前序列

如果我们将单精度浮点数作为参数传递给该方法,则此方法会将单精度浮点数插入到当前序列。

在此示例中,我们使用值“float”实例化StringBuilder 类。使用insert() 方法,我们在指定的偏移量索引 3处插入单精度浮点数“12.89f”

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("float");
      System.out.println("The given string is: " + sb);
      //initialize the offset and float values
      int offset = 3;
      float f = 12.89f;
      System.out.println("The initialize offset and float values are: " + offset + " and " + f);
      //using the insert() method
      System.out.println("After insert float value the string is: " + sb.insert(offset, f));
   }
}

输出

执行上述程序后,将产生以下结果:

The given string is: float
The initialize offset and float values are: 3 and 12.89
After insert float value the string is: flo12.89at

示例:将长整数插入到当前序列

如果我们将长整数作为参数传递给该方法,则 insert() 方法会将长整数值插入到此序列。

在以下程序中,我们使用值“long”创建StringBuilder 类的对象。然后,使用insert() 方法,我们尝试在指定的偏移量索引 1处插入长整数值“123456”

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("long");
      System.out.println("The given string is: " + sb);
      //initialize the offset and long values
      int offset = 1;
      long l = 123456;
      System.out.println("The initialize offset and long values are: " + offset + " and " + l);
      //using the insert() method
      System.out.println("After insert long value the string is: " + sb.insert(offset, l));
   }
}

输出

以下是上述程序的输出:

The given string is: long
The initialize offset and long values are: 1 and 123456
After insert long value the string is: l123456ong

示例:将字符串插入到当前序列

如果我们将字符串作为参数传递给该方法,则此方法会将字符串插入到当前序列。

在此程序中,我们创建了一个值为“Tutorials”StringBuilder。然后,使用insert() 方法,我们在指定的偏移量索引 9处将字符串“point”插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {   
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("Tutorials");
      System.out.println("The given string is: " + sb);
      //initialize the offset and string values
      int offset = 9;
      String str = "Point";
      System.out.println("The initialize offset and string values are: " + offset + " and " + str);
      //using the insert() method
      System.out.println("After insert string value the string is: " + sb.insert(offset, str));
   }
}

输出

上述程序产生以下输出:

The given string is: Tutorials
The initialize offset and string values are: 9 and Point
After insert string value the string is: TutorialsPoint

示例:将对象插入到当前序列

如果我们将对象作为参数传递给该方法,则 insert() 方法会将对象插入到当前序列。

在以下程序中,我们使用值“Object”实例化StringBuilder 类。然后,我们创建了一个值为“this is an ”的对象。使用insert() 方法,我们尝试在指定的偏移量索引 0处将对象值插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //instantiate of the StringBuilder class
      StringBuilder sb = new StringBuilder("Object");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char sequence values
      int offset = 0;
      Object obj = "this is an ";
      System.out.println("The initialize offset and object values are: " + offset + " and '" + obj + "'");
      //using the insert() method
      System.out.println("After insert object value, the string is: " + sb.insert(offset, obj));
   }
}

输出

以下是上述程序的输出:

The given string is: Object
The initialize offset and object values are: 0 and 'this is an '
After insert object value, the string is: this is an Object

示例:将字符插入到当前序列

如果我们将字符序列作为参数传递给该方法,则此方法会将字符序列插入到此序列。

在此程序中,我们使用值“char”创建StringBuilder 类的对象。使用insert() 方法,我们尝试在指定的偏移量索引 4处将字符序列“this is char sequence”插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("char");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char sequence values
      int offset = 4;
      CharSequence cs = "sequence";
      System.out.println("The initialize offset and char sequence are: " + offset + " and " + cs);
      //using the insert() method
      System.out.println("After insert char sequence value the string is: " + sb.insert(offset, cs));
   }
}

输出

执行上述程序后,将产生以下结果:

The given string is: char
The initialize offset and char sequence values are: 4 and sequence
After insert char sequence value, the string is: charsequence

示例:将CharSequence插入到当前序列

如果我们将字符序列、起始索引和结束索引作为参数传递给该方法,则 insert() 方法会将字符序列插入到当前序列。

在以下示例中,我们创建了一个值为“Hello”StringBuilder。然后,使用insert() 方法,我们尝试在偏移量索引 2处插入字符序列“char sequence”,并指定起始索引 0 和结束索引 10

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("Hello");
      System.out.println("The given string is: " + sb);
      //initialize the offset, startIndex, endIndex, and char sequence values
      int offset = 2;
      int startIndex = 0;
      int endIndex = 10;
      CharSequence cs = "char sequence";
      System.out.println("The initialize offset, startIndex, endIndex, and char sequence values are: " + offset + ", " + startIndex +", " + endIndex + ", and '" + cs + "'");
      //using the insert() method
      System.out.println("After insert char sequence value, the string is: " + sb.insert(offset,cs, startIndex, endIndex));
   }
}

输出

执行上述程序后,将产生以下结果:

The given string is: Hello
The initialize offset, startIndex, endIndex, and char sequence values are: 2, 0, 8, and 'char sequence'
After insert char sequence value, the string is: Hechar seqllo

示例:将字符插入到当前序列

如果我们将字符作为参数传递给该方法,则此方法会将字符插入到此序列。

在此程序中,我们使用值“char”创建StringBuilder 类的对象。然后,使用insert() 方法,我们尝试在指定的偏移量索引 4处将字符“A”插入到此序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("char");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char values
      int offset = 4;
      char ch = 'A';
      System.out.println("The initialize offset and char values are: " + offset + " and " + ch);
      //using the insert() method
      System.out.println("After insert char value, the string is: " + sb.insert(offset,ch));
   }
}

输出

以下是上述程序的输出:

The given string is: char
The initialize offset and char values are: 4 and A
After insert char value, the string is: charA

示例:将字符数组插入到当前序列

如果我们将字符数组作为参数传递给该方法,则 insert() 方法会将字符数组插入到当前序列。

在以下程序中,我们使用值“char array”实例化StringBuilder 类。然后,我们创建了一个值为{‘A’, ’B’, ’C’}的字符数组。使用insert() 方法,我们尝试在指定的偏移量索引 0处将字符数组插入到当前序列。

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("char array");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char array values
      int offset = 0;
      char ch[] = {'A', 'B', 'C'};
      System.out.println("The initialize offset value is: " + offset);
      System.out.print("The char array: ");
      System.out.println(ch);
      //using the insert() method
      System.out.println("After insert char array value, the string is: " + sb.insert(offset,ch));
   }
}

输出

上述程序产生以下输出:

The given string is: char array
The initialize offset values is: 0
The char array: ABC
After insert char array value, the string is: ABCchar array

示例

如果我们将字符数组、索引和长度作为参数传递给该方法,则此方法会将字符数组插入到此序列。

在以下程序中,我们创建了一个值为“ABC”StringBuilder。然后,我们创建了一个值为{‘E’,’F’,’G’,’H’}的字符数组。使用insert() 方法,我们尝试在偏移量索引 0处将字符数组值插入到此序列,并指定索引值为 1 和长度值为 2

package com.tutorialspoint.StringBuilder;
public class Insert {
   public static void main(String[] args) {
      //create an object the StringBuilder class
      StringBuilder sb = new StringBuilder("ABC");
      System.out.println("The given string is: " + sb);
      //initialize the offset, index, length, and char array values
      int offset = 0;
      int index = 1;
      int length = 2;
      char ch[] = {'D', 'E', 'F', 'H'};
      System.out.println("The initialize offset, index, length values are: " + offset + ", " + index + ", and " + length);
      System.out.print("The char array: ");
      System.out.println(ch);
      //using the insert() method
      System.out.println("After insert char array value, the string is: " + sb.insert(index,ch, offset, length));
   }
}

输出

执行上述程序后,将生成以下输出:

The given string is: ABC
The initialize offset, index, length values are: 0, 1, and 2
The char array: DEFH
After insert char array value, the string is: ADEBC
java_lang_stringbuilder.htm
广告

© . All rights reserved.