Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char object is ignored in string concatenation

I noticed that the fully qualified name of an object I had written was coming back funny. While stepping through my ToString() method, I noticed that when it came to concatenating the string, a character object was consistently being left out of that process.

Here's a step through of what's happening

Before Before

After After

Where Char seperator = ':';

Here's the code of my tostring function:

public String ToString(Representaion rep)
        {
            String toReturn = "kuid";
            Char separator = ':';

            switch (rep)
            {
                case Representaion.Colons:
                    break;
                case Representaion.Underscores:
                    separator = '_';
                    break;
                case Representaion.UCROnly:
                    toReturn = userID + ":" + contentID;
                    toReturn += revision == 0 ? "" : ":" + revision;
                    return toReturn;
            }

            toReturn += version == 0 ? "" : version.ToString();
            toReturn += separator + userID + separator + contentID;
            toReturn += revision == 0 ? "" : separator + revision.ToString();
            return toReturn;
        }

Where you have

private byte version;
private int userID;
private int contentID;
private byte revision;

And one case may look like this:

enter image description here

Already, looking in the locals panel, it seems like VS is getting a string other than what I think it would.

I put in another ToString function to handle a call without parameters (which it does by calling the parametrized function with Representation.Colons):

public override string ToString()
        {
            return this.ToString(KUID.Representaion.Colons);
        }

Can anyone tell why I'm not getting what I think I should be getting? (Expected result: kuid2:72938:40175:2)

like image 904
gcode Avatar asked Dec 23 '12 05:12

gcode


People also ask

Can you concatenate a char to a string in Java?

We can convert a char to a string object in java by concatenating the given character with an empty string .

How do you concatenate characters in Java?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

How do you concatenate strings in SAP?

Instead of CONCATENATE, it is usually also possible to use string expressions for elementary fields. These expressions enable concatenations using either concatenation operators && or embedded expressions in string templates. To concatenate rows in an internal table, the predefined function concat_lines_of can be used.

How do you concatenate strings in Ruby?

Concatenating strings or string concatenation means joining two or more strings together. In Ruby, we can use the plus + operator to do this, or we can use the double less than operator << .


3 Answers

I copy pasted your code and it works fine

enter image description here

like image 38
sa_ddam213 Avatar answered Sep 25 '22 15:09

sa_ddam213


Now that you've posted more of your program the problem is obvious. Char plus int is not string. Remember,

string += char + int + char + int

means:

string = string + (((char + int ) + char) + int)

And when you add an int to a char, you get an int: 'a' + 2 produces the integer character code corresponding to 'c', not the string "a2".

You're getting some crazy integer by adding the user id to the colon char.

Concatenating strings like this is a bad practice for exactly the reason you have run into. Instead, say:

return string.Format("kuid{0}{1}{2}{3}{4}{5}{6}",
    version, separator, userID, separator, contentID, 
    revision == 0 ? "" : separator.ToString(),
    revision == 0 ? "" : revision.ToString());

Or, even better, use a StringBuilder object to build a complicated string.

Incidentally, this illustrates an interesting point about the language:

a += b + c;

does not mean

a = (a + b) + c; 

It means

a = a + (b + c);

which as we've seen, might have a different type analysis! Had you said:

string = string + char + int + char + int

then that would have been analyzed as

string = ((((string + char) + int) + char ) + int;

Which does make everything a string.

like image 188
Eric Lippert Avatar answered Sep 23 '22 15:09

Eric Lippert


The problem happens, because the expression to the right is not a string expression. You are working with characters and integers which are not automatically converted to a string, unless they are used within a string expression. You can make it a string expression by starting with a string (here an empty string):

toReturn += "" + separator + userID + separator + contentID;
like image 23
Olivier Jacot-Descombes Avatar answered Sep 23 '22 15:09

Olivier Jacot-Descombes