2010-05-24 82 views
0

這是我正在努力完成的。我有一個對象從字符串描述 回來的數據庫。此說明最長可達1000 個字符,但我們只想顯示一個簡短的視圖。所以我編碼 以下,但我在實際刪除 單詞的數量之後正則表達式找到單詞的總數後遇到問題。有沒有人 有很好的方式來顯示比Regex.Matches更少的單詞?如何根據字數去除單詞

謝謝!

if (!string.IsNullOrEmpty(myObject.Description)) 
{ 
    string original = myObject.Description; 
    MatchCollection wordColl = Regex.Matches(original, @"[\S]+"); 
    if (wordColl.Count < 70) // 70 words? 
    { 
     uxDescriptionDisplay.Text = 
      string.Format("<p>{0}</p>", myObject.Description); 
    } 
    else 
    {       
     string shortendText = original.Remove(200); // 200 characters? 
     uxDescriptionDisplay.Text = 
       string.Format("<p>{0}</p>", shortendText); 
    } 
} 

編輯:

所以這是我得到的工作對我自己:

else 
{ 
    int count = 0; 
    StringBuilder builder = new StringBuilder(); 
    string[] workingText = original.Split(' '); 
    foreach (string word in workingText) 
    { 
     if (count < 70) 
     { 
      builder.AppendFormat("{0} ", word); 
     } 
     count++; 
    } 
     string shortendText = builder.ToString(); 
} 

它不漂亮,但它的工作。我會說這是一個非常天真的做法。感謝所有的建議!

回答

3

一種方法,以下。這是提供你要嚴格按字數,而不是字符數。

if (wordColl.Count > 70) 
{ 
    foreach (var subWord in wordColl.Cast<Match>().Select(r => r.Value).Take(70)) 
    { 
     //Build string here out of subWord 
    } 
} 

我沒有用一個簡單的Console.WriteLine與正則表達式,你的身體問題(這是70點的話,就轉出)的測試。

+0

這實際上是我用來替換我的kludgey代碼的那個。比我工作的時間短得多,並且仍然使用我收集的文字。 – Chris 2010-05-24 19:52:56

5

我會選擇嚴格的字符數而不是字數,因爲你可能碰巧有很多長單詞。

我可能會做類似的信息(僞)

if text.Length > someLimit 
    find first whitespace after someLimit (or perhaps last whitespace immediately before) 
    display substring of text 
else 
    display text 

可能的代碼實現:如果你使用至少C#3.0,會像在A LINQ

string TruncateText(string input, int characterLimit) 
{ 
    if (input.Length > characterLimit) 
    { 
     // find last whitespace immediately before limit 
     int whitespacePosition = input.Substring(0, characterLimit).LastIndexOf(" "); 

     // or find first whitespace after limit (what is spec?) 
     // int whitespacePosition = input.IndexOf(" ", characterLimit); 

     if (whitespacePosition > -1) 
      return input.Substring(0, whitespacePosition); 
    } 
    return input; 
} 
+0

可能比我想要做的更多,但很值得了解。 – Chris 2010-05-24 19:53:44

1

您可以使用Regex Capture Groups來保存匹配並在稍後訪問它。

對於您的應用程序,我建議而不是簡單地分裂空格字符串並返回數組的第n個元素:

if (!string.IsNullOrEmpty(myObject.Description)) 
{ 
    string original = myObject.Description; 
    string[] words = original.Split(' '); 
    if (words.Length < 70) 
    { 
     uxDescriptionDisplay.Text = 
      string.Format("<p>{0}</p>", original); 
    } 
    else 
    {       
     string shortDesc = string.Empty; 
     for(int i = 0; i < 70; i++) shortDesc += words[i] + " "; 
     uxDescriptionDisplay.Text = 
      string.Format("<p>{0}</p>", shortDesc.Trim()); 
    } 
} 
+0

好評分和徽章:) – serg 2010-05-24 19:45:18

0

你想刪除200個字符或啓動第200字符截斷?當您致電original.Remove(200)時,您正在將第200個字符處的截尾索引編入索引。這是你如何使用刪除()一定數目的字符刪除:

string shortendText = original.Remove(0,200); 

這開始於第一個字符,並刪除200開頭的那一個。我認爲這不是你在縮短描述之後所要做的。這只是使用Remove()的正確方法。

而不是使用正則表達式matchcollections爲什麼不只是分割字符串?這很容易和直接。您可以將分隔符設置爲空格字符並按此方式拆分。不知道這是否能完全解決您的需求,但它可能會。我不確定您的數據在說明中的樣子。但是你這樣分開:

String[] wordArray = original.Split(' '); 

從那裏你可以用wordArray的Length屬性值來確定字數。

+0

其實,我不想處理角色數。我真的在努力處理物理字數。但謝謝你的建議。 – Chris 2010-05-24 19:54:55

+0

在分割成單詞數組後,Length屬性可以獲得單詞數。 – jlafay 2010-05-24 21:27:46

0

如果我是你,我會走人,因爲你的文字中可能有許多單詞或許多長單詞。

直到字符< =您的限制,然後找到下一個空格,然後將這些字符添加到一個新的字符串(可能使用SubString方法)或採取這些字符並添加幾個句號,然後創建一個新的字符串後者可能是我認爲不合理的。