marwan akhandaf marwan akhandaf - 23 days ago 5
C# Question

c# : 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 don't recognise Startswith.

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 spot where i wan't to use the method startswith.


this is the code where i wan't 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
            }
        }
Comments