Computer >> Máy Tính >  >> Lập trình >> C#

Phương thức C # Trim ()

Phương thức Trim () trong C # được sử dụng để trả về một chuỗi mới trong đó tất cả các lần xuất hiện ở đầu và cuối của một tập hợp các ký tự được chỉ định từ chuỗi hiện tại đều bị xóa.

Cú pháp

 public string Trim (); public string Trim (params char [] trimChars); 

Ở trên, tham số trimChars là một mảng các ký tự Unicode để loại bỏ hoặc null.

Ví dụ

 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 (sau khi xén) =" + 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 ("\ n \ nString2 =" + str2); Console.WriteLine ("String2 ToUpper =" + str2.ToUpper (new CultureInfo ("en-US", false))); Console.WriteLine ("String2 Substring from index2 =" + str2.Substring (0, 5)); }} 

Đầu ra

 String1 =JackSparrow! String1 (after trim) =JackSparrow! String1 ToUpper =JACKSPARROW! String1 Chuỗi con từ index4 =String2 =@ # $ PQRSTUV! String2 ToUpper =@ # $ PQRSTUV! Chuỗi con String2 từ index2 =@ 
 

Ví dụ

 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 (sau khi xén) =" + str.Trim (arr)); }} 

Đầu ra

 String =JackSparrow! String (after trim) =ckSparrow!