|
-
Mar 16th, 2011, 12:19 PM
#1
Thread Starter
Fanatic Member
Re: .NET fixed length strings
 Originally Posted by Edgemeal
So if you wanted to use a fixed length string you'd have to make a class or function and call it on your string everytime you changed it?
Maybe something like... ?....
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' fix to 8 char length
Debug.Print(">" & FixStrLength("1234567890", 8) & "<")
' fix to 4 char length
Debug.Print(">" & FixStrLength("123", 4) & "<")
End Sub
Private Function FixStrLength(strIn As String, MaxLength As Integer) As String
If strIn.Length > MaxLength Then ' remove excess chars.
Return strIn.Substring(0, MaxLength)
ElseIf strIn.Length < MaxLength Then 'add needed spaces to fill to length
Return strIn & New String(" "c, MaxLength - strIn.Length)
Else
Return strIn 'nothing to do
End If
End Function
Hi Edgemeal,
Yeah I thought about writing my own class to do this, but I just figured something like this is common and built into the .net framework.
Thanks,
Strick
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|