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

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

Phương thức ToCharArray () trong C # được sử dụng để sao chép các ký tự trong trường hợp này sang mảng ký tự Unicode.

Cú pháp

Cú pháp như sau -

 public char [] ToCharArray (); public char [] ToCharArray (int seeknIndex, int len); 

Ở trên, seeknIndex là vị trí bắt đầu của một chuỗi con trong trường hợp này. Len là độ dài của chuỗi con trong trường hợp này.

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ -

 using System; public class Demo {public static void Main (String [] args) {string str1 ="Notebook"; string str2 ="Ultrabook"; char [] arr1 =str1.ToCharArray (); char [] arr2 =str2.ToCharArray (); Console.WriteLine ("String1 =" + str1); Console.WriteLine ("String1 ToUpperInvariant =" + str1.ToUpperInvariant ()); Console.WriteLine ("String1 Substring from index4 =" + str1.Substring (4, 4)); Console.Write ("Mảng ký tự ... String1 ="); for (int i =0; i  

Đầu ra

 String1 =NotebookString1 ToUpperInvariant =NOTEBOOKString1 Chuỗi con từ mảng index4 =bookCharacter ... String1 =N o t e b o o kString2 =UltrabookString2 ToUpperInvariant =ultrabookString2 Chuỗi con từ mảng index2 =UltraCharacter ... 

Ví dụ

 using System; public class Demo {public static void Main (String [] args) {string str1 ="Chào mừng!"; string str2 ="Thisisit!"; char [] arr1 =str1.ToCharArray (3,2); char [] arr2 =str2.ToCharArray (2,2); Console.WriteLine ("String1 =" + str1); Console.WriteLine ("String1 ToUpperInvariant =" + str1.ToUpperInvariant ()); Console.WriteLine ("String1 Substring from index4 =" + str1.Substring (4, 4)); Console.Write ("Mảng ký tự ... String1 ="); for (int i =0; i  

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 String1 =Welcome! String1 ToUpperInvariant =WELCOME! String1 Substring from index4 =ome! Character array ... String1 =c oString2 =Thisisit! String2 ToUpperInvariant =thisisit! String2 Chuỗi con từ index2 =mảng ThisiCharacter ... / pre>