C# Trim()方法
C# 中的 Trim() 方法用於返回一個新字串,其中刪除了當前字串中指定字元集的所有前導和尾隨出現。
語法
public string Trim (); public string Trim (params char[] trimChars);
上面,trimChars 引數是要刪除的 Unicode 字元陣列,或 null。
示例
using System;
using System.Globalization;
public class Demo {
public static void Main(String[] args) {
string str1 = " JackSparrow!";
string str2 = " @#$PQRSTUV!";
Console.WriteLine("String1 = "+str1);
Console.WriteLine("String1 (after trim) = "+str1.Trim());
Console.WriteLine("String1 ToUpper = "+str1.ToUpper(new CultureInfo("en-US", false)));
Console.WriteLine("String1 Substring from index4 = " + str1.Substring(2, 2));
Console.WriteLine("
String2 = "+str2);
Console.WriteLine("String2 ToUpper = "+str2.ToUpper(new CultureInfo("en-US", false)));
Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5));
}
}輸出
String1 = JackSparrow! String1 (after trim) = JackSparrow! String1 ToUpper = JACKSPARROW! String1 Substring from index4 = String2 = @#$PQRSTUV! String2 ToUpper = @#$PQRSTUV! String2 Substring from index2 = @
示例
using System;
public class Demo {
public static void Main(String[] args) {
string str = "JackSparrow!";
char[] arr = { 'J', 'a'};
Console.WriteLine("String = "+str);
Console.WriteLine("String (after trim) = " + str.Trim(arr));
}
}輸出
String = JackSparrow! String (after trim) = ckSparrow!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP