Search code examples
c#stringsplitdelimiter

Splitting the array with / slash


From debugger in my string of arrays I am getting this:

"/mercedes-benz/190-class/1993/" class="canonicalLink" data-qstring="?sub=sedan">1993

I wish to split text after each '/' and get it in string[], here is my effort:

Queue<string> see = new Queue<string>(); //char[] a = {'\n '};
       List<car_facts> car_fact_list = new List<car_facts>();
       string[] car_detail;

       foreach (string s in car)
       {
         
            MatchCollection match = Regex.Matches(s, @"<a href=(.+?)</a>",
            RegexOptions.IgnoreCase);

            // Here we check the Match instance.
            foreach(Match mm in match)
            {
                // Finally, we get the Group value and display it.
                string key = mm.Groups[1].Value;
                //key.TrimStart('"');
                //key.Trim('"');
                key.Trim();
                
                // @HERE: I tried with string.Split as well and tried many combinations of separators
                car_detail = Regex.Split(key, "//");
                
                see.Enqueue(key);
            }
}

In car_detail[0] I get this "$[link]">$[title]

from this string:

"/mercedes-benz/190-class/1993/" class="canonicalLink" data-qstring="?sub=sedan">1993

Solution

  • It's not clear why you're using a double slash here...

    string[] details = key.Split('/');
    

    should work fine. (Note that forward-slashes don't have to be escaped in C#.) For example:

    using System;
    
    class Test
    {
        static void Main()
        {
            string text = "/mercedes-benz/190-class/1993/";
            string[] bits = text.Split('/');
            foreach (string bit in bits)
            {
                Console.WriteLine("'{0}'", bit);
            }
        }
    }
    

    Output:

    ''
    'mercedes-benz'
    '190-class'
    '1993'
    ''
    

    The empty strings are due to the leading and trailing slashes. If you want to avoid those, you can use

    string[] details = key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
    

    Notes:

    • car_facts is a highly unconventional name in C#. Normally you'd have something like CarFacts (or potentially just Car, CarInfo etc). Likewise car_fact_list would normally be carFactList or something similar.

    • This code doesn't do what you expect it to:

      key.Trim();
      

      Strings are immutable in .NET - so Trim() returns a reference to a new string rather than changing the contents of the existing one. You may want:

      key = key.Trim();
      
    • You're currently assigning a value to car_detail but never using it. Why?

    • Parsing HTML using regular expressions is a really bad idea in general. Consider using HTML Agility Pack or something similar.