1

Trying to use a .replace function to take out the breaks (<br> and </br>) though having trouble with the code I have been given.

Here is what I am working with.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
var rawStream = sr.ReadToEnd();
sr.Close();
var myBuilder = new StringBuilder();
myBuilder.AppendLine("Employee Name : " + rawStream.Between("<span id=\"Label_DisplayFullName\">", "</span>"));
myBuilder.AppendLine("Title: " + rawStream.Between("<span id=\"Label_Title\">", "</span>"));
myBuilder.AppendLine("Location : " + rawStream.Between("<span id=\"Label_Location\">", "</span>"));
myBuilder.AppendLine("Department : " + rawStream.Between("<span id=\"Label_Department\">", "</span>"));
myBuilder.AppendLine("Group: " + rawStream.Between("<span id=\"Label_Group\">", "</span>"));
myBuilder.AppendLine("Office Phone : " + rawStream.Between("<span id=\"Label_IntPhoneNumber\">", "</span>"));
myBuilder.AppendLine("Mobile Phone : " + rawStream.Between("<span id=\"Label_BusMobile\">", "</span>"));
richTextBox1.Text = myBuilder.ToString();

Though I understand the function should be like so:

public string Replace( string oldValue, string newValue )

I just dont understand how this works in my code as I dont really have a "string" but I have a string builder.

Any assistance would be huge.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
jstoveld
  • 29
  • 4
  • Just write myBuilder.Replace("old","new"); – CSharpie Apr 23 '14 at 16:37
  • 2
    If your aim is to parse html and take some stuff out you should consider using [html agility pack](https://htmlagilitypack.codeplex.com/) – oleksii Apr 23 '14 at 16:39
  • Please when showing sample code avoid unknown methods like `Between` - it is not very clear if you have problem with code that is not shown OR that unknown method is problem itself... And if you are using `String.Replace` to avoid [using regex to parse Html](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) use proper parser like HtmlAgilityPack instead. – Alexei Levenkov Apr 23 '14 at 16:59

5 Answers5

2

What does all your StringBuilder stuff have to do with removing the breaks? Your string is in the rawStream variable (quite badly named) (that's what ReadToEnd() gives you), so you'd just:

rawStream = rawStream.Replace("<br>", "");
rawStream = rawStream.Replace("<br />");
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • I'm a rookie with C# so my naming convention is probably far from standard. Let me test your code out - looks like it might be on point. – jstoveld Apr 23 '14 at 16:39
  • It's okay, we've all been there before. :) – Gigi Apr 23 '14 at 16:39
  • I tested this out and it works if I put it before the myBuilder.AppendLine command stack. Thanks a ton! This was driving me wild! Cheers! – jstoveld Apr 23 '14 at 18:05
  • Works! Im happy! (But this is what worked : rawStream = rawStream.Replace("
    ", " "); rawStream = rawStream.Replace("
    ", " ");
    – jstoveld Apr 23 '14 at 18:07
1

There are two things you could do:

On the one hand, you do assemble your string with a StringBuilder, however, you eventually convert the contents of that string builder into a string when you call myBuilder.ToString(). That is where you could invoke Replace:

richTextBox1.Text = myBuilder.ToString().Replace("<br>", "").Replace("</br>", "");

Alternatively, StringBuilder has a Replace method of its own, so you could invoke that before transforming the string builder contents into a string:

myBuilder.Replace("<br>", "");
myBuilder.Replace("</br>", "");

Note that the latter can alternatively be invoked in a chained fashion, as well, though that is arguably less readable:

richTextBox1.Text = myBuilder.Replace("<br>", "").Replace("</br>", "").ToString();
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
0

Can't you just put it after the ToString() call?

richTextBox1.Text = myBuilder.ToString().Replace("string1", "string2");

As mentioned in the comments you can also call it on the stringbuilder object itself.

richTextBox1.Text = myBuilder.Replace("string1", "string2").ToString();
Smeegs
  • 9,151
  • 5
  • 42
  • 78
0

Since replace returns string you can chain them

rawStream = rawStream.Replace("<br>","").Replace("<br/>","").Replace("<br />","");
Reza
  • 18,865
  • 13
  • 88
  • 163
0

Try this.

richTextBox1.Text = myBuilder.Replace("<br>", String.Empty).Replace("</br>", String.Empty).ToString();

Or

var rawStream = sr.ReadToEnd().Replace("<br>", String.Empty).Replace("</br>", String.Empty);
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36