用/斜线分割数组 [英] Splitting the array with / slash

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

问题描述

从我的数组字符串中的调试器我得到这个:

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

"/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 = {'
 '};
       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);
            }
}

car_detail[0] 我得到这个 "$[link]">$[title]

来自这个字符串:

"/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 的东西(或者可能只是 CarCarInfo 等).同样,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.

这段代码没有达到你的预期:

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

key.Trim();

字符串在 .NET 中是不可变的 - 所以 Trim() 返回对 new 字符串的引用,而不是更改现有字符串的内容.你可能想要:

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 是一个非常糟糕的主意.考虑使用 HTML Agility Pack 或类似的东西.

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

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

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