劈开/斜线阵列 [英] Splitting the array with / slash

查看:84
本文介绍了劈开/斜线阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的阵列我得到这个字符串的调试器

from debugger in my string of arrays I am getting this

/奔驰/ 190级/ 1993 /级=canonicalLink数据的QString =?子=轿车> 1993年

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

我想之后的每个'/'分割文本,并得到它的String [],这里是我的心血

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();

                **car_detail = Regex.Split(key, "//");**//I tried with strin.Split as well and tried many combination of seperator ,

                see.Enqueue(key);
            }

}

在car_detail [0]我得到这个$ [链接]> $ [标题]

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

此string 1993年

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

推荐答案

它为什么您使用的是双斜杠这里...不清楚

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

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

应该可以正常工作。 (请注意,正向斜杠的的都在C#中被转义。)例如:

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);
        }
    }
}

输出:

''
'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);

注:


  • car_facts 是在C#高度非常规的名字。通常情况下,你不得不像 CarFacts (或可能只是 CarInfo 等)。同样 car_fact_list 通常会 carFactList 或类似的东西。

  • 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.

这code没有你指望它什么:

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

key.Trim();

字符串是不可变的.NET中 - 这样 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();


  • 您目前正在分配一个值 car_detail ,但从来没有使用它。为什么呢?

  • You're currently assigning a value to car_detail but never using it. Why?

    解析HTML使用常规的前pressions是一般非常糟糕的主意。请考虑使用 HTML敏捷性包或类似的东西。

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

    这篇关于劈开/斜线阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆