C#中Append()⽤法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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论