2012-03-01 21 views
-1

我嘗試使用以下代碼檢索「標題」的值:LINQ:字符串函數從左邊

private void GetTweets_Click(object sender, RoutedEventArgs e) 
    { 
     WebClient client = new WebClient(); 

     client.DownloadStringCompleted += (s, ea) => 
     { 
      XDocument doc = XDocument.Parse(ea.Result); 
      XNamespace ns = "http://www.w3.org/2005/Atom"; 

      var items = from item in doc.Descendants(ns + "entry") 
       select new Tweet() 
       { 
        Title = item.Element(ns + "title").Value, 

        Image = new Uri((from XElement xe in item.Descendants(ns + "link") 
         where xe.Attribute("type").Value == "image/png" 
         select xe.Attribute("href").Value).First<string>()), 
       }; 
      foreach (Tweet t in items) 
      { 
       _tweets.Add(t); 
      } 
     }; 

     client.DownloadStringAsync(new Uri("https://twitter.com/statuses/user_timeline/[username].atom?count=10")); 
    } 

我能夠檢索鳴叫的名單,但是,我想刪除由「標題」值顯示的前16個字符。

是否有辦法在這裏使用的子字符串函數? 謝謝。

+3

您是否嘗試過使用的子功能,爲你自己建議?如果是這樣,發生了什麼? – 2012-03-01 16:28:12

+0

你看過字符串的刪除函數嗎?它需要startindex和count參數。沒有幫助嗎? 這裏是[參考](http://msdn.microsoft.com/en-us/library/d8d7z2kk.aspx) – AnarchistGeek 2012-03-01 16:27:50

回答

2

假設item.Element(ns + "title").Valuestring,您應該可以使用String.Substring(Int32) method

Title = item.Element(ns + "title").Value.Substring(16), 

請注意,這將拋出一個異常,如果標題的長度少於16個字符,所以它很可能是最好的測試,第一。

Title = item.Element(ns + "title").Value.Length > 16 
     ? item.Element(ns + "title").Value.Substring(16) 
     : item.Element(ns + "title").Value, 
+0

這應該是'... Value.Substring(16)';該參數指定要包含在返回的子字符串中的第一個字符的索引。 – phoog 2012-03-01 16:41:42

+0

@phoog - 好,趕上,編輯。謝謝。 – 2012-03-01 16:46:35