Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

c# - 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 = {'
 '};
       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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...