Splitting an array with / slash

from the debugger in my line of arrays I get this


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

I want to split the text after each '/' and get it in the 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(); **car_detail = Regex.Split(key, "//");**//I tried with strin.Split as well and tried many combination of seperator , see.Enqueue(key); } 

}

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

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

+4
source share
1 answer

It is unclear why you are using double slash here ...

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

should work fine. (Note that in C # you do not need to flush forward slashes.) 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); } } } 

Conclusion:

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

Empty lines are associated with leading and trailing slashes. If you want to avoid this, you can use

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

Notes:

  • car_facts is a very unconventional name in C #. Usually you have something like CarFacts (or potentially just Car , CarInfo , etc.). Similarly, car_fact_list will usually be carFactList or something similar.

  • This code does not do what you expect from it:

     key.Trim(); 

    Strings are immutable in .NET - so Trim() returns a link to a new line, rather than replacing the contents of an existing one. You can:

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

  • Parsing HTML using regular expressions is a really bad idea overall. Think using the HTML Agility Pack or something similar.

+12
source

Source: https://habr.com/ru/post/1401046/


All Articles