C#中Append()用法
时间:2011-03-15 来源:茹果し☆涛気
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