📜  C#| ToCharArray()方法

📅  最后修改于: 2021-05-29 18:18:39             🧑  作者: Mango

在C#中, ToCharArray()是一个字符串方法。该方法用于将字符从一个指定的字符串在当前实例复制到Unicode字符阵列或在当前实例将Unicode字符阵列的指定子字符。可以通过更改传递给它的参数数量来重载此方法。

句法:

public char[] ToCharArray()
or
public char[] ToCharArray(int startIndex, int length)

解释:

  • public char [] ToCharArray()方法将不带参数。此方法将返回字符数组。字符数组元素将包含字符串的各个字符。因此,基本上,此方法会将字符串的每个字符复制到字符数组。字符串的第一个字符将在字符数组中最后一个字符的索引零拷贝将在指数Array.Length复制- 1.当我们创建一个字符数组中字符的字符串,然后它会调用字符串( Char [])构造函数。
  • public char [] ToCharArray(int startIndex,int length)方法将采用两个参数startIndex ,用于指定子字符串的起始位置,其类型为System.Int32 。另一个参数是length ,用于指定子字符串的长度,其类型为System.Int32 。 startIndex参数从零开始,这意味着字符串实例中第一个字符的索引可以为零。

重要事项:

  • 如果startIndex或length小于零或(startIndex + length)大于当前字符串实例的长度,则public char [] ToCharArray(int startIndex,int length)方法可以给出异常ArgumentOutOfRangeException
  • 如果指定的长度为零,则返回的数组将为空,并且长度为零。如果当前或该实例为null或空字符串(“”),则返回的数组将为空,长度为零。

以下是演示上述方法的程序:

  • 程序1:为了说明ToCharArray()方法:
    // C# program to demonstrate
    // ToCharArray() method
    using System;
    class Geeks {
        
        // Main Method
        public static void Main()
        {
      
            String str = "GeeksForGeeks";
      
            // copy the string str to chars 
            // character array & it will start
            // copy from 'G' to 's', i.e. 
            // beginning to ending of string
            char[] chars = str.ToCharArray();
      
            Console.WriteLine("String: " + str);
            Console.Write("Character array :");
      
            // to display the resulted character array
            for (int i = 0; i < chars.Length; i++)
                Console.Write(" " + chars[i]);
        }
    }
    
    输出:
    String: GeeksForGeeks
    Character array : G e e k s F o r G e e k s
    
  • 程序2:为了说明ToCharArray(int startIndex,int length)方法:
    // C# program to demonstrate
    // ToCharArray(int startIndex, int length)
    // method
    using System;
    class Geeks {
      
        // Main Method
        public static void Main()
        {
            String str = "GeeksForGeeks";
      
            // copy the string str to chars 
            // character array & it will 
            // start copy from 'F' to 'r' i.e.
            // copy from startindex of string
            // is 5 upto (startindex+length-1) i.e.
            // 5 + (3-1) has to copy
            char[] chars1 = str.ToCharArray(5, 3);
      
            Console.WriteLine("String: " + str);
            Console.WriteLine("\nCharacter array 1:");
      
            // to display the resulted character array
            for (int i = 0; i < chars1.Length; i++)
                Console.WriteLine("Index " + i + " : " + chars1[i]);
      
            // copy the string str to chars 
            // character array & it will 
            // start copy from 'F' to 'k' i.e.
            // copy from startindex of string
            // 5 upto (startindex+length-1) i.e.
            // 5 + (7-1)
            char[] chars2 = str.ToCharArray(5, 7);
      
            Console.Write("\nCharacter array 2:");
      
            // to display the resulted character 
            // array using foreach loop
            foreach(char c in chars2)
                Console.Write(" " + c);
        }
    }
    
    输出:
    String: GeeksForGeeks
    
    Character array 1:
    Index 0 : F
    Index 1 : o
    Index 2 : r
    
    Character array 2: F o r G e e k
    

参考 :

  • https://msdn.microsoft.com/zh-CN/library/system。字符串.tochararray1
  • https://msdn.microsoft.com/zh-CN/library/system。字符串.tochararray2