本文整理汇总了C#中System.String.Trim方法的典型用法代码示例。如果您正苦于以下问题:C# String.Trim方法的具体用法?C# String.Trim怎么用?C# String.Trim使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Trim方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
char[] charsToTrim = { '*', ' ', '\''};
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed\n {0}\nto\n '{1}'", banner, result);
输出:
Trimmmed *** Much Ado About Nothing *** to 'Much Ado About Nothing'
示例2: Main
//引入命名空间
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()).Trim();
Console.WriteLine("The result is " + name + ".");
// The following is a 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.
}
}
示例3: String.Trim()
//引入命名空间
using System;
class MainClass
{
public static void Main()
{
string myString18 = '(' + " Whitespace ".Trim() + ')';
Console.WriteLine("'(' + \" Whitespace \".Trim() + ')' = " + myString18);
string myString19 = '(' + " Whitespace ".TrimStart() + ')';
Console.WriteLine("'(' + \" Whitespace \".TrimStart() + ')' = " + myString19);
string myString20 = '(' + " Whitespace ".TrimEnd() + ')';
Console.WriteLine("'(' + \" Whitespace \".TrimEnd() + ')' = " + myString20);
}
}