Splitting the array with / slash

前端 未结 1 1609
时光说笑
时光说笑 2020-12-06 19:59

from debugger in my string of arrays I am getting this


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

I w

相关标签:
1条回答
  • 2020-12-06 20:42

    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.

    0 讨论(0)
提交回复
热议问题