質問

I'm trying to take a string and split it. However, whenever I use fullName.Split Visual Studio says System.Array does not contain a definition for Split.

This is my main method so far.

   public static void Main(string[] args)
    {
        string inValue;
        int noNames;
        string[] names = new string[100];
       // find number of names
        Console.WriteLine("Enter the number of names: ");
        inValue = Console.ReadLine();
        int.TryParse(inValue, out noNames);
        string[] fullName = new string[noNames];
        for (int i = 0; i < fullName.Length; i++)
        {
            string[] name = fullName.Split(' '); //error appears here
        }
    }

What's strange is that I was able to write another program shortly before this that uses the Split method. That program had no issues. I'm not sure if there is something wrong with my code, or an error with Visual Studio. Can anyone assist me in solving this error? The program isn't complete, if that matters.

役に立ちましたか?

解決

You need to call it on the element of the array, not the array itself.. So it would be:

string[] name = fullName[i].Split(' ');

他のヒント

You are trying to split an array, not a string. Arrays cannot be split this way using a certain character like strings

try this

public static void Main(string[] args)
    {
        string inValue;
        int noNames;
        string[] names = new string[100];
       // find number of names
        Console.WriteLine("Enter the number of names: ");
        inValue = Console.ReadLine();
        int.TryParse(inValue, out noNames);
        string[] fullName = new string[noNames];
        for (int i = 0; i < fullName.Length; i++)
        {
            string[] name = fullName[i].Split(' '); //error appears here
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top