开发者

How to get the value of a System.String object instead of returning "System.String[]"

I am working on a file parser, and this bit of code is not giving me what I want. Before I go any farther, I should mention that I did not write this program, I am only editing the source to fix this specific problem. Also, I can compile the code, so that is not a problem (you know how downloaded programs always have compile errors). Here's the code.

case EsfValueType.Binary4E: //System.String[]
{
    int size = (int)(this.reader.ReadUInt32() - ((uint)this.reader.BaseStream.Position));
    var strings = new string[size / 4];
    for (int i = 0; i < size / 4; i++)
        strings[i] = this.stringValuesUTF16[this.reader.ReadUInt32()];
    esfValue.Value = strings.ToString();
    break;
}

Now, I added the .ToString(); part to the above line, but it made no difference. The problem is that esfValue.Value ends up with System.String[] as it's value, and I want the value of the System.String object. If you can make sense out of this and tell me what is wrong, it would be appreciated.

The program name is 开发者_如何学运维ESF Editor 1.4.8.0.


case EsfValueType.Binary4E: //System.String[]
{
    int size = (int)(this.reader.ReadUInt32() - ((uint)this.reader.BaseStream.Position));
    var strings = new StringBuilder();
    for (int i = 0; i < size / 4; i++)
    {
        strings.Append(this.stringValuesUTF16[this.reader.ReadUInt32()]); //or AppendLine, depending on what you need
    }
    esfValue.Value = strings.ToString();
    break;
}


The strings variable is an array of strings - the Array class does not override the default ToString() implementation which returns the type of the object.

You need to concatenate all the strings in the array - either looping and concatenating or using LINQ and assign the resulting string to esfValue.Value. Of course, this assumes you want the values all in one string, one after the other.


Your issue is that strings isn't a single string, its an array of strings. As a result your call to ToString is calling Object.ToString(), which returns the type of the object.

Maybe you want something like

esfValue.Value = strings.Aggregate((acc, next) => acc + next)

which will simply concatenate all the strings together.


When you do a .ToString() on a class that doesn't override the .ToString() base method to return a custom string (which string[] doesn't), you're always going to get the type's namespace/class as the result.

Arrays, in and of themselves, don't have values. What value are you trying to get? Are you trying to join the array into a single, character-delimited string? If so, this would work:

esfValue.Value = string.Join(",", strings);

Just replace the , with whatever character you want to delimit the array with.


I think you just need to join the string values contained in the string array. In order to do so, you need to call String.Join and pass the string separator and the string array. It returns a single System.String.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜