String concanation bug??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Casper

    #1

    String concanation bug??

    I am seeing some really strange behavior in what should be basic
    language constucts. Sometimes, when forming a new string (a) by
    concanating other strings (b + c), only the first substring (b) gets
    into this new string (a).

    More specifically, I am concanating elements I extract from the
    Arraylist type. I run .ToString() on these extractions so this should
    not pose the problem.

    Never the less, when concanating and trying to use this concanation as
    argument for methods, it will fail in cases where the first element is
    composed of alphanumeric characters - strangely enough, concanation
    using values (also made ToSting() though) works fine.

    // Nick (string) + IP address (string) + Port (int) yeilds ONLY the
    nick!
    for(int i = 0; i < PeerListGUID.Co unt; i++)
    lstPeers.Items. Add(PeerListNic k[i].ToString() +
    PeerListIP[i].ToString() + "@" + PeerListPort[i].ToString());

    // IP address (string) + Port (int) + Nick (string) yeilds desired
    result
    for(int i = 0; i < PeerListGUID.Co unt; i++)
    lstPeers.Items. Add(PeerListIP[i].ToString() + "@" +
    PeerListPort[i].ToString() + " " + PeerListNick[i].ToString());

    ....debugging shows its not my arrays which are "out of sync". Its
    simply the concatenations that goes wrong!

    Any help would be greatly appreciated,
    Casper Bang, Denmark
  • Jon Skeet [C# MVP]

    #2
    Re: String concanation bug??

    Casper <casper@jbr.d k> wrote:[color=blue]
    > I am seeing some really strange behavior in what should be basic
    > language constucts. Sometimes, when forming a new string (a) by
    > concanating other strings (b + c), only the first substring (b) gets
    > into this new string (a).[/color]

    I'm pretty sure you'll find that what you're seeing isn't necessarily
    what's going on. For instance, a null character (0) in the string can
    confuse the debugger in VS 2002.

    I suggest you try to write a short but complete program which
    demonstrates the problem. See
    http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
    that. The first thing to do is have a look at what the variable below
    looks like in the debugger:

    using System;

    public class Test
    {
    static void Main()
    {
    string x = "Hello\0the re";
    }
    }

    If it only looks like "Hello" then chances are the same problem is
    coming up in your real code, and you've got nulls there that you
    weren't expecting. Does the nickname part come from some code which is
    giving null-terminated strings?

    I very much doubt that it's actual string concatenation which is
    failing.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...