Java StringBuilder append()方法及实例

Java StringBuilder append()方法及实例

java.lang.StringBuilder.append() 方法用于将一些参数的字符串表示追加到序列中。通过传递各种类型的参数,有13种方式/形式可以使用append()方法。

  1. StringBuilder append( boolean a ) : java.lang.StringBuilder.append( boolean a )是Java中的一个内置方法,用于将布尔参数的字符串表示法追加到一个给定的序列。

语法 :

public StringBuilder append(boolean a)

参数: 该方法接受一个布尔类型的单一参数a,指的是要追加的布尔值。

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

示例:
输入:
string_buffer = “We are Indians”
boolean a = true

输出:We are Indians true

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// StringBuilder append(boolean a)
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sb1 = new 
                      StringBuilder("Welcome to Geeksforgeeks ");
        System.out.println("输入: " + sb1);
  
        // Appending the boolean value
        sb1.append(true);
        System.out.println("输出:" + sb1);
  
        System.out.println();
  
        StringBuilder sb2 = new StringBuilder("We fail- ");
        System.out.println("输入: " + sb2);
  
        // Appending the boolean value
        sb2.append(false);
        System.out.println("输出:" + sb2);
    }
}

输出:

输入: Welcome to Geeksforgeeks 
输出:Welcome to Geeksforgeeks true

输入: We fail- 
输出:We fail- false
  1. java.lang.StringBuilder.append( char a ): 这是Java中的一个内置方法,用于将char参数的字符串表示附加到给定的序列中。char参数被追加到这个StringBuilder序列的内容中。

语法 :

public StringBuilder append(char a)

参数: 该方法接受一个单一的参数a,它是Char值,其字符串表示将被附加。

返回值: 该方法在执行附加操作后返回一个字符串对象。

示例 :
输入 :
StringBuilder = I love my Country
char a = A

输出:I love my Country A

下面的程序说明了java.lang.StringBuilder.append(char a)方法。

// Java program to illustrate the
// java.lang.StringBuilder.append(char a)
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
          
        StringBuilder sbf = new 
                       StringBuilder("Welcome geeks!");
                System.out.println( sbf);
  
        /* Here it appends the char argument as
        string to the StringBuilder */
        sbf.append('T');
        System.out.println("Result after"+
                                 " appending = " + sbf);
  
      
        sbf = new StringBuilder("hello world-");
                System.out.println(sbf);
        /* Here it appends the char argument as
        string to the String Builder */
        sbf.append('#');
        System.out.println("Result after appending = " 
                                                         + sbf);
    }
}

输出:

Welcome geeks!
Result after appending = Welcome geeks!T
hello world-
Result after appending = hello world-#
  1. StringBuilder append( char[] astr ): java.lang.StringBuilder.append( char[] astr )是一个内置的方法,它将char数组参数的字符串表示附加到这个StringBuilder序列。

语法 :

public StringBuilder append(char[] astr)

参数: 该方法接受一个单一的参数astr,它是Char序列,其字符串表示将被附加。

返回值: 该方法在执行附加操作后返回一个字符串对象。

示例:
输入 :
StringBuffer = I love my Country
char[] astr = ‘I’, ‘N’, ‘D’, ‘I’, ‘A’

输出:I love my Country INDIA

下面的程序说明了java.lang.StringBuilder.append( char[] astr )方法。

// Java program to illustrate the
// java.lang.StringBuilder.append(<em>char[] astr</em>)
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
          
        StringBuilder sbf = new 
                      StringBuilder("We are geeks  ");
        System.out.println(sbf);
  
        // Char array
        char[] astr = new char[] 
                          { 'G', 'E', 'E', 'k', 'S' };
  
        /* Appends string representation of char
                   array to this String Builder */
        sbf.append(astr);
    System.out.println("Result after"+
                            " appending = " + sbf);
          
        sbf = new StringBuilder("We are  -");
                System.out.println(sbf);
  
        // Char array
        astr = new char[] { 'a', 'b', 'c', 'd' };
  
        /* Appends string representation of char 
                   array to this StringBuilder */
        sbf.append(astr);
        System.out.println("Result after appending = " + sbf);
    }
}

输出:

We are geeks  
Result after appending = We are geeks  GEEkS
We are  -
Result after appending = We are  -abcd
  1. StringBuilder append( char[] cstr, int iset, int ilength ) : 该方法将char数组参数的子数组的字符串表示附加到这个序列中。从索引iset开始的char数组cstr的字符被依次追加到这个序列的内容中。这个序列的长度以ilength的值增加。

语法 :

public StringBuilder append(char[] cstr, int iset, int ilength)

参数:该方法接受三个参数。

* cstr - 这是指Char序列。
* iset - 这是指要追加的第一个字符的索引。
* ilength - 这是指要追加的字符的数量。

返回值。该方法在执行追加操作后返回一个字符串对象。
下面的程序说明了java.lang.StringBuilder.append(char[] cstr, int iset, int ilength) 方法。

// Java program to illustrate the
// append(char[] cstr, int iset, int ilength)
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sb = new 
                              StringBuilder("Geeks");
        System.out.println("String Builder "+
                                   "before = " + sb);
  
        char[] cstr = new char[] 
          { 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's','q','q' };
  
        /* appends the string representation of char array 
           argument to this String Builder with offset initially 
           at index 0 and length as 8 */
        sb.append(cstr, 0, 8);
  
        // Print the String Builder after appending
        System.out.println("After appending String Builder = " + sb);
    }
}

输出:

String Builder before = Geeks
After appending String Builder = GeeksforGeeks
  1. StringBuilder append( double a ): 这个方法只是将double参数的字符串表示法追加到这个StringBuilder序列中。

语法 :

public StringBuilder append(double val)

参数: 该方法接受一个参数val,它指的是要附加的字符串表示的十进制值。

返回值: 该方法在执行附加操作后返回一个字符串对象。
示例 :
输入 :
StringBuffer = my Country
Double a = 54.82

输出:my Country 54.82

下面的程序说明了java.lang.StringBuilder.append(double val)方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        System.out.println("We are geeks and its really ");
        StringBuilder sbf = new 
              StringBuilder("We are geeks and its ");
  
        // Char array
        Double astr = new Double(36.47);
  
        /* Here it appends string representation of Double 
           argument to this StringBuilder*/
        sbf.append(astr);
        System.out.println("Result after appending = " + sbf);
  
        System.out.println("We are lost -");
        sbf = new StringBuilder("We are lost -");
  
        astr = new Double(27.38);
  
        /* Here it appends string representation of Double  
           argument to this StringBuilder*/
        sbf.append(astr);
        System.out.println("Result after appending = " + sbf);
    }
}

输出:

We are geeks and its really 
Result after appending = We are geeks and its 36.47
We are lost -
Result after appending = We are lost -27.38
  1. StringBuilder append( float val ): 该方法将浮动参数的字符串表示法追加到该序列中。

语法 :

public StringBuilder append(float val)

参数: 该方法接受一个单一的参数val,它是要附加的字符串表示的浮点数。

返回值: StringBuilder.append( float val )方法在执行操作后返回一个字符串对象的引用。

示例 :
输入 :
StringBuilder = I love my Country
float a = 5.2

输出:I love my Country 5.2

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        System.out.println("We are geeks and its really ");
        StringBuilder sbf = new 
                    StringBuilder("We are geeks and its ");
  
        Float astr = new Float(6.47);
  
        /* Here it appends string representation of 
           Float argument to this StringBuilder */
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
  
        System.out.println("We are lost -");
        sbf = new StringBuilder("We are lost -");
  
        astr = new Float(27.38);
  
        // Here it appends string representation of Float
        // argument to this String Builder
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
    }
}

输出:

We are geeks and its really 
Result after appending = We are geeks and its 6.47
We are lost -
Result after appending = We are lost -27.38
  1. StringBuilder append( int val ) 这个方法只是将int参数的字符串表示法追加到这个StringBuilder序列中。
    语法 :
public StringBuilder append(int val)

参数: 该方法接受一个单一的参数val,它是一个整数值,应该对其进行操作。

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

示例 :
输入 :
StringBuilder = I love my Country
int a = 55

输出:I love my Country 55

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        System.out.println("We are geeks and its really ");
        StringBuilder sbf = new 
                    StringBuilder("We are geeks and its ");
  
        Integer astr = new Integer(827);
  
        /* Here it appends string representation of 
           Integer argument to this StringBuilder*/
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
  
        System.out.println("We are lost -");
        sbf = new StringBuilder("We are lost -");
  
        astr = new Integer(515);
  
        // Here it appends string representation of Integer
        // argument to this StringBuilder
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
    }
}

输出:

We are geeks and its really 
Result after appending = We are geeks and its 827
We are lost -
Result after appending = We are lost -515
  1. StringBuilder append( Long val ) : 这个方法只是将长参数的字符串表示法追加到这个StringBuilder序列中。

语法 :

public StringBuilder append(Long val)

参数: 该方法接受一个单一参数val,即长值。

返回值: 该方法在执行完追加操作后返回一个字符串对象。

示例 :
输入 :
StringBuilder = I love my Country
Long a = 591995

输出:I love my Country 591995

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        System.out.println("We are geeks and its really ");
        StringBuilder sbf = new 
                    StringBuilder("We are geeks and its ");
  
        Long astr = new Long(827);
  
        /* Here it appends string representation of 
           Long argument to this StringBuilder*/
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
  
        System.out.println("We are lost -");
        sbf = new StringBuilder("We are lost -");
  
        astr = new Long(515);
  
        /* Here it appends string representation of Long 
           argument to this StringBuilder*/
        sbf.append(astr);
        System.out.println("Result after appending = "+sbf);
    }
}

输出:

We are geeks and its really 
Result after appending = We are geeks and its 827
We are lost -
Result after appending = We are lost -515
  1. StringBuilder append( CharSequence a ): 该方法用于将指定的CharSequence追加到该序列中。

语法 :

public StringBuilder append(CharSequence a)

参数: 该方法接受一个单一参数a,即CharSequence值。

返回值: 该方法在执行完追加操作后返回一个字符串对象。

示例 :
输入 :
StringBuilder = “I love my Country”
CharSequence a = ” India”

输出 : I love my Country India

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sbf = new StringBuilder("Geeksfor");
        System.out.println("String Builder = " + sbf);
  
        CharSequence chSeq = "geeks";
  
        // Appends the CharSequence
        sbf.append(chSeq);
  
        // Print the String Builder after appending
        System.out.println("After append = " + sbf);
    }
}

输出:

String Builder = Geeksfor
After append = Geeksforgeeks
  1. StringBuilder append( CharSequence chseq, int start, int end ) : 该方法用于将指定的CharSequence的一个子序列追加到这个StringBuilder中。

语法 :

StringBuilder append(CharSequence chseq, int start, int end)

参数:该方法接受三个参数。

  • chseq(CharSequence)。这指的是CharSequence值。
  • start (Integer): 这指的是要附加的子序列的起始索引。
  • end (Integer): 这指的是要附加的子序列的结束索引。

返回值:该方法在执行完追加操作后返回字符串。

示例 :
输入 :
StringBuilder = Geeksforgeeks
CharSequence chseq = abcd1234
int start = 2
int end = 7

输出 :Geeksforgeekscd123

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sbf = new 
              StringBuilder("We are the ");
        System.out.println("String builder= "
                                      + sbf);
  
        CharSequence chSeq = "wegeekss";
  
        /* It appends the CharSequence with  
           start index 2 and end index 4 */
        sbf.append(chSeq, 2, 7);
  
        System.out.println("After append string"+
                             " buffer = " + sbf);
    }
}

输出:

String builder= We are the 
After append string buffer = We are the geeks
  1. StringBuilder append( Object obj ) : 该方法用于将Object参数的字符串表示法追加到StringBuilder中。

语法 :

StringBuilder append(Object obj)

参数: 该方法接受一个参数obj,它指的是需要附加的对象。

返回值: 该方法在执行追加操作后返回字符串。

下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sbf = new StringBuilder("Geeksfor");
        System.out.println("String Builder = " + sbf);
  
        Object objectvalue = "geeks";
  
        // Here it appends the Object value
        sbf.append(objectvalue);
  
        System.out.println("After appending result is = "+sbf);
    }
}

输出:

String Builder = Geeksfor
After appending result is = Geeksforgeeks
  1. StringBuilder append( String istr ) : 该方法用于将指定的字符串追加到这个StringBuilder中。
    语法 :
StringBuilder append(String istr)

参数:该方法接受一个字符串类型的单一参数istr,它指的是要附加的值。

返回值:该方法返回一个指定的字符串到这个字符序列。
下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sbf = new StringBuilder("Geeksfor");
        System.out.println("String Builder = " + sbf);
  
        String strvalue = "geeks";
  
        // Here it appends the Object value
        sbf.append(strvalue);
  
        System.out.println("After appending result is = "+sbf);
    }
}

输出:

String Builder = Geeksfor
After appending result is = Geeksforgeeks
  1. StringBuilder append( StringBuilder sbf ) : 该方法用于将指定的StringBuilder追加到该序列或StringBuilder。

语法:

public StringBuilder append(StringBuilder sbf)

参数:该方法接受一个参数sbf,指的是要追加的StringBuilder。

返回值:该方法返回StringBuilder到这个序列。
下面的程序说明了java.lang.StringBuilder.append()方法。

// Java program to illustrate the
// java.lang.StringBuilder.append()
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        StringBuilder sbf1 = new StringBuilder("Geeks");
        System.out.println("String Builder 1 = " + sbf1);
  
        StringBuilder sbf2 = new StringBuilder("forgeeks ");
        System.out.println("String Builder 2 = " + sbf2);
  
        // Here it appends String Builder2 to String Builder1
        sbf1.append(sbf2);
  
        System.out.println("After appending the result is = "+sbf1);
    }
}

输出:

String Builder 1 = Geeks
String Builder 2 = forgeeks 
After appending the result is = Geeksforgeeks

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程