2011-10-06 173 views
1

**更新,我回答了我自己的問題,但沒有代表使它正式成立。我將在6個小時內回覆它,直到那時我會在這裏發佈我的答案。從字符串c中刪除單詞#

好了,所以我想通了如何通過我現有的功能之一刪除的話:

public static string RemoveHTML(string text) 
{ 
    text = text.Replace("&nbsp;", " ").Replace("<br>", "\n").Replace("description", "").Replace("INFRA:CORE:", "") 
     .Replace("RESERVED", "") 
     .Replace(":", "") 
     .Replace(";", "") 
     .Replace("-0/3/0", ""); 
     var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>"); 
     return oRegEx.Replace(text, string.Empty); 
} 

------- --------以下原 問題,謝謝大家你的幫助,希望這也可以用於其他人。

我正在研究ASP.NET 4.0 Web應用程序,它的主要目標是轉到MyURL變量中的URL,然後從上到下讀取它,搜索以「description」開頭的所有行,只有在刪除所有HTML標籤時才保留這些標籤。我接下來要做的是從結果後綴中刪除「description」文本,以便我只剩下我的設備名稱。我將如何做到這一點?先謝謝你!

protected void parseButton_Click(object sender, EventArgs e) 
    { 
     MyURL = deviceCombo.Text; 
     WebRequest objRequest = HttpWebRequest.Create(MyURL); 
     objRequest.Credentials = CredentialCache.DefaultCredentials; 
     using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream())) 
     { 
      originalText.Text = objReader.ReadToEnd(); 
     } 

     //Read all lines of file 
     String[] crString = { "<BR>&nbsp;" }; 
     String[] aLines = originalText.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries); 

     String noHtml = String.Empty; 

     for (int x = 0; x < aLines.Length; x++) 
     { 
      if (aLines[x].Contains(filterCombo.SelectedValue)) 
      { 
       noHtml += (RemoveHTML(aLines[x]) + "\r\n"); 

      } 
     } 
     //Print results to textbox 
     resultsBox.Text = String.Join(Environment.NewLine, noHtml); 
    } 
    public static string RemoveHTML(string text) 
    { 
     text = text.Replace("&nbsp;", " ").Replace("<br>", "\n"); 
     var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>"); 
     return oRegEx.Replace(text, string.Empty); 
    } 
+0

我會存儲(編譯)正則表達式在一個靜態變量,這可能會加快進程,並避免內存泄漏和\ n與Environment.NewLine – slfan

回答

4

我回答我自己的問題,但沒有代表正式成立。我將在6個小時內回覆它,直到那時我會在這裏發佈我的答案。

好了,所以我想通了如何通過我現有的功能之一刪除的話:

public static string RemoveHTML(string text) 
{ 
    text = text.Replace("&nbsp;", " ").Replace("<br>", "\n").Replace("description", "").Replace("INFRA:CORE:", "") 
     .Replace("RESERVED", "") 
     .Replace(":", "") 
     .Replace(";", "") 
     .Replace("-0/3/0", ""); 
     var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>"); 
     return oRegEx.Replace(text, string.Empty); 
} 
0

嘗試這樣的事情,使用LINQ:

List<string> lines = new List<string>{ 
"Hello world", 
"Description: foo", 
"Garbage:baz", 
"description purple"}; 

//now add all your lines from your html doc. 
if (aLines[x].Contains(filterCombo.SelectedValue)) 
{ 
     lines.Add(RemoveHTML(aLines[x]) + "\r\n"); 
} 

var myDescriptions = lines.Where(x=>x.ToLower().BeginsWith("description")) 
          .Select(x=> x.ToLower().Replace("description",string.Empty) 
             .Trim()); 

// you now have "foo" and "purple", and anything else. 

您可能需要調整冒號等

+0

錯誤CS1061:'string'不包含'ToLowerCase'的定義,並且沒有找到接受'string'類型的第一個參數的擴展方法'ToLowerCase'(可以找到缺少使用指令或程序集引用嗎?) – KPS

+0

因爲我首先使用「description」作爲過濾器,所以做這件事最好的地方是什麼。 – KPS

+0

@KPS使用'ToLower()'而不是'ToLowerCase()' – shuniar

0
void Main() 
{ 
    string test = "<html>wowzers description: none <div>description:a1fj391</div></html>"; 
    IEnumerable<string> results = getDescriptions(test); 
    foreach (string result in results) 
    { 
     Console.WriteLine(result); 
    } 

    //result: none 
    //  a1fj391 
} 

static Regex MyRegex = new Regex(
     "description:\\s*(?<value>[\\d\\w]+)", 
    RegexOptions.Compiled); 

IEnumerable<string> getDescriptions(string html) 
{ 
    foreach(Match match in MyRegex.Matches(html)) 
    { 
     yield return match.Groups["value"].Value; 
    } 
} 
0

Adapted From Code Project

string value = "ABC - UPDATED"; 
int index = value.IndexOf(" - UPDATED"); 
if (index != -1) 
{ 
    value = value.Remove(index); 
} 

它將打印ABC沒有- UPDATED

+0

小心這樣做,因爲Remove會將索引中的所有字符移除到字符串末尾。正則表達式或替換將只是做特定的字/字符。 –