C# 移除字串所有的前置和後端空白字元(String.Trim 方法)

C# 移除字串所有的前置和後端空白字元(String.Trim 方法)

C# 移除字串所有的前置和後端空白字元(String.Trim
方法
)


 

資料來源: https://msdn.microsoft.com/zh-tw/library/t97s7bs3(v=vs.100).aspx

 

using
System;

 

public class
Example

{

   public static void Main()

   {

      Console.Write(“Enter
your first name: ”
);

      string firstName = Console.ReadLine();

 

      Console.Write(“Enter
your middle name or initial: ”
);

      string middleName = Console.ReadLine();

 

      Console.Write(“Enter
your last name: ”
);

      string lastName = Console.ReadLine();

 

      Console.WriteLine();

      Console.WriteLine(“You
entered ‘{0}’, ‘{1}’, and ‘{2}’.”
,

                        firstName,
middleName, lastName);

 

      string name = (firstName.Trim() +

+ middleName.Trim()).Trim() +

+

                    lastName.Trim();

      Console.WriteLine(“The
result is ”
+ name + “.”);

   }

}

//
The following is possible output from this example:

//       Enter your first name:    John

//       Enter your middle name or initial:

//       Enter your last name:    Doe

//      

//       You entered ‘   John 
‘, ”, and ‘   Doe’.

//       The result is John Doe

 

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *