BestTechie Forums: How To Create A Fixed Length String In C# Mono - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Create A Fixed Length String In C# Mono


#1 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 20 June 2007 - 07:50 PM

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++

#2 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 22 June 2007 - 08:35 AM

View Posticcaros, 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();

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 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

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
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
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users