Rocks Rocks - 1 year ago 92 Question

Weird string manipulation issue

I am trying to make a series of strings exactly the same length. I am trying to do these steps :

  1. If the string is more than 25 characters, trim the extra.

  2. If it's less than 25, fill it up with spaces.

  3. Make sure that either way the strings are all 25 characters long.

Here is my code (or at least the relevant bit)

If Len(My.Settings.currOrder1) > 25 Then
My.Settings.currOrder1 = Microsoft.VisualBasic.Left(My.Settings.currOrder1, 25)
My.Settings.currOrder1 = My.Settings.currOrder1 + " "
My.Settings.currOrder1 = Microsoft.VisualBasic.Left(My.Settings.currOrder1, 25)
End If

which in my head should work. But if i run that code 3 times with a 6 character input, then 7, then 8, the output I get is this:

enter image description here

which is pretty obviously not shortening anything (the price is simply tacked on the end to show where the string ends). Any ideas?

Answer Source

Try this :

Dim str As String = "Abc"
If str.Length < 25 Then
  str = str.PadRight(25 - str.Length, " "C)
  str = str.Substring(0, 25)
End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download