代码之家  ›  专栏  ›  技术社区  ›  user8098743

合并/优化子字符串语句

  •  0
  • user8098743  · 技术社区  · 7 年前

    string page = wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB");
    int pos;
    while ((pos = page.IndexOf("/watch?v=")) > 0) {
        page = page.Substring(pos);
        page = page.Substring(page.IndexOf("video-time"));
        page = page.Substring(page.IndexOf("aria-label"));
        page = page.Substring(page.IndexOf(@"\u003e") + 6);
        string vt = page.Substring(0, page.IndexOf(@"\u003c"));
        page = page.Substring(page.IndexOf("title=") + 1);
        page = page.Substring(page.IndexOf("title=") + 1);
        page = page.Substring(page.IndexOf("title=") + 1);
        page = page.Substring(page.IndexOf("\\\"") + 2);
        string tt = page.Substring(0, page.IndexOf("\\\"  aria-describedby="));
    }
    

    可悲的是,我不能跳过一些 Substring vt tt 自从 子字符串 总是返回我尝试解决方案的新字符串 StringBuilder :

    System.Text.StringBuilder sb=new System.Text.StringBuilder(wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB"));
    int pos;
    while ((pos = sb.ToString().IndexOf("/watch?v=")) > 0) {
        sb.Remove(0,pos);
        sb.Remove(0,sb.ToString().IndexOf("video-time"));
        sb.Remove(0,sb.ToString().IndexOf("aria-label"));
        sb.Remove(0,sb.ToString().IndexOf(@"\u003e") + 6);
        string vt =sb.ToString(0,sb.ToString().IndexOf(@"\u003c"));
        sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
        sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
        sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
        sb.Remove(0,sb.ToString().IndexOf("\\\"") + 2);
        string tt =sb.ToString(0,sb.ToString().IndexOf("\\\"  aria-describedby="));
    }
    

    ToString() ,确实稍微快一点。

    现在,有没有办法进一步优化这一点?甚至可以让它看起来更好?

    2 回复  |  直到 7 年前
        1
  •  0
  •   user8098743 user8098743    7 年前

    根据@harold的建议

    string page = wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB");
    int pos;
    while ((pos = page.IndexOf("/watch?v=")) > 0) {
        int subPos=pos;
        subPos=page.IndexOf("video-time",subPos);
        subPos=page.IndexOf("aria-label",subPos);
        subPos=page.IndexOf(@"\u003e",subPos);
        subPos+=6;
        string vt=page.Substring(subPos,(subPos=page.IndexOf(@"\u003c",subPos)));
        subPos=page.IndexOf("title=",subPos);
        subPos++;
        subPos=page.IndexOf("title=",subPos);
        subPos++;
        subPos=page.IndexOf("title=",subPos);
        subPos=page.IndexOf("\\\"",subPos);
        subPos+=2;
        string tt=page.Substring(subPos,(subPos=page.IndexOf("\\\"  aria-describedby=", subPos)));
        page=page.Substring(subPos);
    }
    

    似乎比使用 StringBuilder 具有 ToString()

        2
  •  0
  •   user8098743 user8098743    7 年前

    these extension methods

    System.Text.StringBuilder sb=new System.Text.StringBuilder(wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB"));
    int pos;
    while ((pos = sb.ToString().IndexOf("/watch?v=")) > 0) {
        sb.Remove(0,pos);
        sb.Remove(0,sb.IndexOf("video-time"));
        sb.Remove(0,sb.IndexOf("aria-label"));
        sb.Remove(0,sb.IndexOf(@"\u003e") + 6);
        string vt =sb.ToString(0,sb.IndexOf(@"\u003c"));
        sb.Remove(0,sb.IndexOf("title=") + 1);
        sb.Remove(0,sb.IndexOf("title=") + 1);
        sb.Remove(0,sb.IndexOf("title=") + 1);
        sb.Remove(0,sb.IndexOf("\\\"") + 2);
        string tt =sb.ToString(0,sb.IndexOf("\\\"  aria-describedby="));
    }
    

    应该比使用更快 StringBuilder ToString() ,我的测试结果有点不稳定。