Results 1 to 10 of 10
  1. #1
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

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

    Read this after reading the "Edit" section below
    Spoiler:
    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




    after TrimEnd




    I'm fetching 3 records, and maybe because the div I added last isn't included in 'lb_left_inner', I only get one.
    Spoiler:



    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.
    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  2. #2
    122.26.11.85 Zangetsu's Avatar
    Join Date
    Jan 2008
    Location
    Soul Society
    Posts
    6,692

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

    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.
    HTC One V | Nokia 5233 | Nikon D3100 | Apple Ipod Touch 4G | Sound Magic PL21 | Sound Magic ES18

  3. #3
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

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

    Quote Originally Posted by Zangetsu View Post
    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;
                
            }
            
        }
    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  4. #4
    122.26.11.85 Zangetsu's Avatar
    Join Date
    Jan 2008
    Location
    Soul Society
    Posts
    6,692

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

    Quote Originally Posted by RazorbladeXtreme View Post
    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.
    HTC One V | Nokia 5233 | Nikon D3100 | Apple Ipod Touch 4G | Sound Magic PL21 | Sound Magic ES18

  5. #5
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

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

    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.
    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  6. #6
    122.26.11.85 Zangetsu's Avatar
    Join Date
    Jan 2008
    Location
    Soul Society
    Posts
    6,692

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

    ^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
    HTC One V | Nokia 5233 | Nikon D3100 | Apple Ipod Touch 4G | Sound Magic PL21 | Sound Magic ES18

  7. #7
    Master KOD3R nbaztec's Avatar
    Join Date
    Sep 2010
    Location
    New Delhi
    Posts
    342

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

    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.
    AMD Phenom II X6 1055T|MSI 880GMA-E45|MSI 7770|2x2 Corsair DDR3 1333MHz|CM Elite 310|Corsair CX400W|DELL ST2320L
    Myself @ nbaztec.co.in
    Build Your Rig! (XBlade) @ nbaztec.co.in - Build Your Rig!
    My Inner Artist @ nbaztec.co.in - Designs

  8. #8
    122.26.11.85 Zangetsu's Avatar
    Join Date
    Jan 2008
    Location
    Soul Society
    Posts
    6,692

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

    ^that's what I was explaining to OP
    HTC One V | Nokia 5233 | Nikon D3100 | Apple Ipod Touch 4G | Sound Magic PL21 | Sound Magic ES18

  9. #9
    VIP RazorbladeXtreme's Avatar
    Join Date
    May 2008
    Location
    Jaipur
    Posts
    239

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

    Quote Originally Posted by Zangetsu View Post
    ^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.
    Twitter: twitter.com/SharmaTushar
    Facebook: facebook.com/tushar.sharma

  10. #10
    Master KOD3R nbaztec's Avatar
    Join Date
    Sep 2010
    Location
    New Delhi
    Posts
    342

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

    Quote Originally Posted by RazorbladeXtreme View Post
    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.
    AMD Phenom II X6 1055T|MSI 880GMA-E45|MSI 7770|2x2 Corsair DDR3 1333MHz|CM Elite 310|Corsair CX400W|DELL ST2320L
    Myself @ nbaztec.co.in
    Build Your Rig! (XBlade) @ nbaztec.co.in - Build Your Rig!
    My Inner Artist @ nbaztec.co.in - Designs

Similar Threads

  1. Need help with vb connection string
    By arian29 in forum Programming
    Replies: 9
    Last Post: 02-09-2008, 01:55 PM
  2. String.replace
    By estranged12 in forum Programming
    Replies: 2
    Last Post: 09-04-2008, 09:58 AM
  3. C++ String to all conversion.... Help needed...
    By aditya.shevade in forum Programming
    Replies: 8
    Last Post: 06-03-2008, 11:33 PM
  4. cant declare a string variable...
    By geekgod in forum Open Source
    Replies: 13
    Last Post: 12-08-2006, 06:09 AM
  5. asp 'replace' string query!
    By 144 in forum QnA (read only)
    Replies: 1
    Last Post: 30-06-2006, 10:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Close