marwan akhandaf marwan akhandaf - 27 days ago 6
C# Question

How to use Startswith in Visual Studio

I want to use

StartsWith
to print names with the letter that the user gives. But I don't know how to use
StartsWith
in Visual Studio. Visual Studio doesn't recognize it.

ArrayList namen = new ArrayList();
int kosten = 0;
string naam;
Console.Write("voer de namen in: ");
naam = Console.ReadLine();


while (naam.Length > 0)
{
if (namen.Contains(naam) == false)
{
namen.Add(naam);
}

Console.Write("Voer de namen in: ");
naam = Console.ReadLine().ToLower();
}

foreach (string n in namen)
{
Console.WriteLine(n);
kosten += 10;
}

Console.WriteLine(kosten + " euro");
Console.WriteLine("voer een letter in: ");
string letter = Console.ReadLine();


This is the code where I want to use the method
StartsWith
.

Answer

If you want to check a name of the namen ArrayList StartsWith a letter, you can use the following example:

        string letter = Console.ReadLine();
        ArrayList namen = new ArrayList();
        foreach (string name in namen)
        {
            if(name.StartsWith(letter))
            {
                // Yes, the name StartsWith the input letter
                // Do Something
            }
        }