這是我正在努力完成的。我有一個對象從字符串描述 回來的數據庫。此說明最長可達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();
}
它不漂亮,但它的工作。我會說這是一個非常天真的做法。感謝所有的建議!
這實際上是我用來替換我的kludgey代碼的那個。比我工作的時間短得多,並且仍然使用我收集的文字。 – Chris 2010-05-24 19:52:56