Append()用法
Class Preson
{
private string firstName;
private string middletName;
private string lastName; //定義3個私人變數 firstName middleName lastName
Public Preson(string fn,string mn,string ln) //建構函式
{
firstName=fn;
middleName=mn;
lastName=ln;
}
public void displayFull Name() //定義一個方法名
{
StringBuilder FullName =new StringBuilder(); //執行個體化FullName
FullName.Append(firstName); //FullName =FullName+firstName Full為空白 所有只顯示FirstName
FullName.Append(" "); //FullName =FullName +"空格" FullName 已經包含firstName的值
if(middleName[0]!="") //如果中間名的第一數組裡字元非空則向下運行 否則跳出此IF語句
{
FullName.Append("."); //滿足條件則繼續FullName = FullName +"."
FullName.Append(lastname); //FullName= FullName + lastName的值
Console.WriteLine(FullName); //輸出FullName
}
}
public static void Main()
{
Person me =new Person("Bradley","Lee","Jones"); //執行個體化
me.displayFullName(); //調用該函數
}
輸出結果:Bradley L.Jones
解釋:FullName->Bradley 先賦值
FullName->Bradley_ 加一個空格("_"代表空格 不是底線)
FullName->Bradley_L
FullName->Bradley_L.
FullName->Bradley_L.Jones
最後FullName= Bradley L.Jones