Java StringBuilder append()方法

java.lang.StringBuilder.append(int i) 方法追加int参数到此序列的字符串表示形式。

1 语法

public StringBuilder append(int i)

2 参数

i : 这是int值。

3 返回值

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

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java StringBuilder append()方法
 */
import java.lang.*;

public class StringBuilderDemo {

  public static void main(String[] args) {
  
    StringBuilder str = new StringBuilder("tuts ");
    System.out.println("string = " + str);

    // appends the int argument as string to the StringBuilder
    str.append(20);
    // print the StringBuilder after appending
    System.out.println("After append = " + str);
    
    str = new StringBuilder("abcd ");
    System.out.println("string = " + str);
    // appends the int argument as string to the StringBuilder
    str.append(145);
    // print the StringBuilder after appending
    System.out.println("After append = " + str);
  }
}

输出结果为:

string = tuts
After append = tuts 20
string = abcd
After append = abcd 145

 

热门文章

优秀文章