ok I need to create a fixed length string that is 31 char and a null
this is what I am trying
StringBuilder sb = new StringBuilder(name,31);
this.Name = sb.ToString();
Ideals or suiggestions..
how is this done in c++
Page 1 of 1
How To Create A Fixed Length String In C# Mono
Tweet
#2
Posted 22 June 2007 - 08:35 AM
iccaros, on Jun 20 2007, 05:50 PM, said:
ok I need to create a fixed length string that is 31 char and a null
How easily do you need the access the trailing null? If it's just for interop, I believe .NET strings are internally null-terminated so they can be passed to unmanaged code without marshalling.
Quote
StringBuilder sb = new StringBuilder(name,31);
this.Name = sb.ToString();
this.Name = sb.ToString();
No idea if that's guaranteed to work. You probably have to set the StringBuilder's Capacity if you really 31 characters -- the ctor argument is a 'suggestion' -- but I'm not sure that's guaranteed to work, and I don't think there's any guarantee that the length of the string returned by ToString() will be the same as the StringBuilder's Capacity.
Quote
how is this done in c++
char s[32] = "foo"
This post has been edited by jcl: 22 June 2007 - 08:51 AM
#3
Posted 22 June 2007 - 07:22 PM
thanks,
stringbuild does infact shorten the string to the actual length when the ToString() method is used..
I did it this way
EDIt:
Replace the word zero with the number 0, the BB keeps removing my 0 ..
stringbuild does infact shorten the string to the actual length when the ToString() method is used..
I did it this way
string FixedLength(string value)
{
if (string.lenght > 31)
{
string = string.substring(0,31);
string += "\zero";
}
else
(
string = value;
string += value.PadRight(31) + "\zero";
}
return string
}EDIt:
Replace the word zero with the number 0, the BB keeps removing my 0 ..
This post has been edited by iccaros: 22 June 2007 - 07:25 PM
Page 1 of 1

Sign In »
Register Now!
Help

Back to top
MultiQuote