Appending string issue

Last post 05-09-2008 3:26 AM by stmarti. 6 replies.

Sort Posts:

  • Appending string issue

    05-08-2008, 4:30 PM
    • Loading...
    • dba123
    • Joined on 12-13-2003, 12:04 AM
    • Posts 986

     What the hell could be wrong

                    absolutepath = absolutepath == null ? ("/" + folders[i] + "/") : absolutepath + folders[i] + "/";

    outputs the folder[i] text just fine but leaves out the leading "/" 

    C# Web Developer

    When is Microsoft going to get rid of VB.NET!
  • Re: Appending string issue

    05-08-2008, 4:38 PM
    • Loading...
    • pixelsyndicate
    • Joined on 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 982

    by setting absolutepath = to something you are erasing the previous values

    try

    absolutepath += absolutepath == null ? ("/" + folders[i] + "/") : absolutepath + folders[i] + "/";

    or

    absolutepath = absolutepath + absolutepath == null ? ("/" + folders[i] + "/") : absolutepath + folders[i] + "/";

    or

    if(absolutepath == null)
      absolutepath = ("/" + folders[i] + "/");
    else
      absolutepath += folders[i] + "/";

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://pixelsyndicate.com/ps/
  • Re: Appending string issue

    05-08-2008, 4:39 PM
    • Loading...
    • huenemeca
    • Joined on 08-02-2002, 10:19 AM
    • Posts 124

     Are you sure that absolutepath is null?  If it is equal to string.Empty then the else path will be taken and it would look like it went down the first path but missed the /.

  • Re: Appending string issue

    05-08-2008, 4:44 PM
    • Loading...
    • pixelsyndicate
    • Joined on 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 982

    huenemeca is right... not knowing what your absolutepath represents makes it kinda obtuse.

    if its a string type, look for this:

    if(absolutepath == string.empty) { }

     

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://pixelsyndicate.com/ps/
  • Re: Appending string issue

    05-08-2008, 5:15 PM
    • Loading...
    • che3358
    • Joined on 09-25-2003, 10:23 AM
    • Cleveland, OH
    • Posts 700

    Seems should be absolutepath = (absolutepath == null || absolutepath.Trim() == "") ? ("/" + folders[i] + "/") : absolutepath + folders[i] + "/";

  • Re: Appending string issue

    05-09-2008, 2:47 AM
    • Loading...
    • Svante
    • Joined on 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

    dba123:
    What the hell could be wrong

    Not using the appropriate methods and being to fond of one-liners perhaps? ;-) Try something like this:

    if (String.IsNullOrEmpty(absolutePath))
    {
      absolutePath = "/";
    }
    absolutePath = VirtualPathUtility.AppendTrailingSlash(absolutePath + folders[i]);
    
     

     

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: Appending string issue

    05-09-2008, 3:26 AM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 542

    What about this?

    absolutepath = ( absolutepath ?? "/" ) + folders[i] + "/";

    Sorry not works when absolute path is empty string, the best and safest I think is 

    che3358:
    Seems should be absolutepath = (absolutepath == null || absolutepath.Trim() == "") ? ("/" + folders[i] + "/") : absolutepath + folders[i] + "/";

Page 1 of 1 (7 items)