none
How to make this work, I cant call the string in a different method, please help RRS feed

  • Question

  •  static string[] x = {"Бургас","Варна","Силистра","Търговище","Ловеч",
                 "Кърджали","Пловдив","Благоевград","Пазарджик","Асеновград","Ботевград","Велико Търново","Габрово",
                "Димитровград","Копривщица",  "Крумовград",  "Кърджали" };
            static void Print(string[] a)
            {           
                for (int i = 0; i < a.Length; i++)
                {
                    string element = a[i];
                    Console.WriteLine(i+1+": "+a[i]);
                }
            }
            public static void SolveB(string[] a)
            {

                string part = "град";
                bool show =x.Contains(part);
                int firstCharacter = x.IndexOf(part);

                Console.WriteLine("First occurrence: {0}", firstCharacter,part);


            }

      }
    }

      I want the string x to be recongnised by the indexOf so it can make the words which contains that part of the word but i cant make it work                    
    Saturday, November 24, 2018 10:23 PM

All replies

  • Note that x is not a string as you claim, but rather it is an array of strings. The array does not contain any method called "IndexOf", and therefore your code produces a compilation error in the line where you write  int firstCharacter = x.IndexOf(part). The rest of the code compiles correctly.

    You should review what you are trying to do, and ensure that you call IndexOf on a single string, not on the whole array of strings.

    Saturday, November 24, 2018 10:45 PM
    Moderator
  • Is there any way to call the x in the method with indexOf so it can check those words. im new and dont have a real idea what im exactly doing and how to do it 100% correctly
    Saturday, November 24, 2018 10:59 PM
  • You should use Linq.

    http://www.dotnetlearners.com/linq/linq-to-string-array-with-example.aspx

    Sunday, November 25, 2018 12:36 AM
  • using System;
    using System.Linq;
          

           string[] x = {"one","two","three","four" };

           var myList = x.ToList();

                var position =  myList.IndexOf("two");

                Console.WriteLine(position);


    Sunday, November 25, 2018 3:30 AM
  • this one would help me if i can understand how it works in 100% so i cant use it in my code, but anyway thanks i wont stop trying :D
    Sunday, November 25, 2018 12:23 PM
  • Hi Salamandar,

    Thank you for posting here.

    If your question has been solved, please make the useful reply as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 27, 2018 3:11 AM
    Moderator