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
廣告