[ASP.net][C#] String.TrimEnd() method

RBX

In the zone
Read this after reading the "Edit" section below :)
I am encountering a problem which I think is related to TrimEnd method.

I have following markup

Code:
<div class="listbox-content">
                <h4 class="listbox-heading">
                    Latest Properties</h4>
                <div class="lb-left" id="lb_left" runat="server">
                    <div id="lb_left_inner" runat="server">
                        <div class="inner-cont">
                            <asp:Image ID="Image1" ImageUrl="~/images/slider-img02.jpg" runat="server" />
                        </div>
                        <div class="inner-cont">
                            <asp:Image ID="Image3" ImageUrl="~/images/slider-img01.jpg" runat="server" />
                        </div>
                        <div class="inner-cont">
                            <asp:Image ID="Image5" ImageUrl="~/images/slider-img03.jpg" runat="server" />
                        </div>
                    </div>
                </div>

		<%-- some other div, commented --%>
            </div>

I'm taking HTML of div 'lb_left_inner', trimming '</div>' from its end, adding some HTML to it and putting it all back inside the div 'lb_left'.

Code:
protected void Page_Load(object sender, EventArgs e) {
        BLL rbll = new BLL();
        char[] endDiv = new char[] { '<', '/', 'd', 'i', 'v', '>' };
        try {
            DataTable dt = rbll.SelectAllProperties();
            foreach (DataRow dr in dt.Rows) {
                string[] arr = new string[dt.Columns.Count];
                int i = 0;
                foreach (DataColumn dc in dt.Columns) {
                    arr[i] = dr[dc].ToString();
                    i++;
                }
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                lb_left_inner.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(sb)));
                string s = sb.ToString();                
                for (int x = 0; x < 5; x++)
                    s = s.TrimEnd(endDiv);
                this.lb_left.InnerHtml = s + "<div class=\"inner-cont\"><img src=\"" + arr[16].TrimStart(new char[] {'~','/'}) + "\" runat=\"server\"/><h3 class=\"room-type\">Rent a commercial room</h3><p>Jaipur, IX ker<br />105 000 Ft/mo<br />75 m2 / 3 bed / 1 liv / 1 bath</p><a class=\"more\">More Details</a></div>" + "</div>";
            }
        } catch (Exception exc) {
            Response.Write("<h1>" + exc.Message + "</h1>");
        } finally {
            rbll = null;
            //sb = null;
        }
    }

My inspection show that TrimEnd is not working as expected (or maybe I'm missing something else).

variable s before TrimEnd
*i.imgur.com/fztPd.png



after TrimEnd
*i.imgur.com/0AxOp.png
I'm fetching 3 records, and maybe because the div I added last isn't included in 'lb_left_inner', I only get one.
*i.imgur.com/mKrgo.png


EDIT:
I decided to use this, and I think this does the job
s = s.Substring(0, s.Length - 6);

still, I can get only 1 division added to the previous content.
 

Zangetsu

I am the master of my Fate.
Hey u have used for loop for TrimEnd

for (int x = 0; x < 5; x++)
s = s.TrimEnd(endDiv);

no need of for loop..remove that & it will work.
 
OP
RBX

RBX

In the zone
Hey u have used for loop for TrimEnd

for (int x = 0; x < 5; x++)
s = s.TrimEnd(endDiv);

no need of for loop..remove that & it will work.

I figured that because my TrimStart() did work, oddly enough TrimEnd() doesn't. Anyway if 5 iterations didn't make a change to the string, I guess 1 won't.

Driving away from the Topic title, Trimming the end is least of concern for now, which I can do now using substring, not too efficient, but does the job for now; the problem for now is that lb_left_inner.RenderControl returns the same HTML over and over, without any change being reflected, so after the loop, only 1 div is being added.


EDIT:
I misjudged what InnerHtml does, the following code works for me, I didn't need to set InnerHtml of lb_left, lb_left_inner.InnerHtml doesn't indicate what's inside its tags, rather its whole body.

Code:
protected void Page_Load(object sender, EventArgs e) {

        BLL rbll = new BLL();
        try {
            DataTable dt = rbll.SelectAllProperties();
            foreach (DataRow dr in dt.Rows) {
                string[] arr = new string[dt.Columns.Count];
                int i = 0;
                foreach (DataColumn dc in dt.Columns) {
                    arr[i] = dr[dc].ToString();
                    i++;
                }
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                lb_left_inner.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(sb)));
                string s = sb.ToString();
                this.lb_left_inner.InnerHtml = s + "<div class=\"inner-cont\"><img src=\"" + arr[16].TrimStart(new char[] { '~', '/' }) + "\" runat=\"server\"/><h3 class=\"room-type\">Rent a commercial room</h3><p>Jaipur, IX ker<br />105 000 Ft/mo<br />75 m2 / 3 bed / 1 liv / 1 bath</p><a class=\"more\">More Details</a></div>";
            }
        } catch (Exception exc) {
            Response.Write("<h1>" + exc.Message + "</h1>");
        } finally {
            rbll = null;
            
        }
        
    }
 

Zangetsu

I am the master of my Fate.
I figured that because my TrimStart() did work, oddly enough TrimEnd() doesn't. Anyway if 5 iterations didn't make a change to the string, I guess 1 won't.

using a for loop u r doing 25 iterations...bcoz u have used a char array of length which has its own pattern match iterations so 5*5 = 25 iterations.

& believe removing the for loop will do the work (tried & tested) instead of a hard-coded value of 6 in substring.
 
OP
RBX

RBX

In the zone
I tried it at first, but for some reason didn't work, and doesn't work even now.
Current code has totally eliminated the need of Trimming, but I hope I'll get it right in future if need arises. :)
 

Zangetsu

I am the master of my Fate.
^I don't know why it didn't work for u.
I copy/pasted your above code & it was trimming the </div> when for loop is not used
 

nbaztec

Master KOD3R
I can vouch for Zangetsu, TrimEnd(charArray) removes trailing occurrences of all characters present in the array. Tried and tested. The reason it might be failing could be because of a stray whitespace. But that's just a wild guess.
 
OP
RBX

RBX

In the zone
^that's what I was explaining to OP :|

You see, if first iteration removes the </div> then even if doesn't remove any other </div> in consecutive iterations, the string I receive after 1/5 iterations should be different from the original, which wasn't the case.
 

nbaztec

Master KOD3R
You see, if first iteration removes the </div> then even if doesn't remove any other </div> in consecutive iterations, the string I receive after 1/5 iterations should be different from the original, which wasn't the case.
Well there can be many reasons for that, but a tried & tested library function failing, would be my - and I can not emphasize this enough - very last guess.

The TrimEnd() would remove only the first occurrence of '</div>' after that the spaces would cause it to return. I'm still guessing a stray space, \t or a CR/LF at the end is messing it up.
 
Top Bottom