limiting number of characters - pad

  • Thread starter Thread starter Brock
  • Start date Start date
B

Brock

Thanks in advance... I have a string that I'm building that needs to
write to a file the last name, first name and middle initial in this
format with a maximum of 20 characters. Two examples:

Brenda J Lanier
Lanier,Brenda J

' note that this name is only 15 characters, with the space and
comma, after formatting so I'm safe

Edward M Berksweller:
Berksweller,Garrett M

' note that this name has 21 characters, with the space and comma,
so to stay within the 20 char limit it needs to write as:
Berksweller,Garrett
with the space being the 20th character... it's OK for me to loose the
middse initial

My best guess is this for the code:

FieldString = (emp.LastName & "," & emp.FirstName & "
" & emp.MI)
EmpList.Write(FieldString.PadRight(20, " "c))

But I don't think this will work. Any ideas? thanks!!!
 
Brock said:
Thanks in advance... I have a string that I'm building that needs to
write to a file the last name, first name and middle initial in this
format with a maximum of 20 characters. Two examples:

Brenda J Lanier
Lanier,Brenda J

' note that this name is only 15 characters, with the space and
comma, after formatting so I'm safe

Edward M Berksweller:
Berksweller,Garrett M

' note that this name has 21 characters, with the space and comma,
so to stay within the 20 char limit it needs to write as:
Berksweller,Garrett
with the space being the 20th character... it's OK for me to loose the
middse initial

My best guess is this for the code:

FieldString = (emp.LastName & "," & emp.FirstName & "
" & emp.MI)
EmpList.Write(FieldString.PadRight(20, " "c))

But I don't think this will work. Any ideas? thanks!!!

One way would be to
first put the words in a string in the order you want,
then get the length of the string,
then if its length is < 20 use the whole string.
else use the first 20 characters using the subString (0, 20) method.

Dim string2Use as String = myString.Substring(0, 20)
 
Thanks in advance... I have a string that I'm building that needs to
write to a file the last name, first name and middle initial in this
format with a maximum of 20 characters. Two examples:

Brenda J Lanier
Lanier,Brenda J

' note that this name is only 15 characters, with the space and
comma, after formatting so I'm safe

Edward M Berksweller:
Berksweller,Garrett M

' note that this name has 21 characters, with the space and comma,
so to stay within the 20 char limit it needs to write as:
Berksweller,Garrett
with the space being the 20th character... it's OK for me to loose the
middse initial

My best guess is this for the code:

FieldString = (emp.LastName & "," & emp.FirstName & "
" & emp.MI)
EmpList.Write(FieldString.PadRight(20, " "c))

But I don't think this will work. Any ideas? thanks!!!

Your explanation of what you want is confusing, and it didn't help
that you used an example in which the middle initial just put you over
the 20 character limit.

If your requirement is to use the first 20 characters of
"lastname,firstname m" regardless of where the break occurs in the
name (what if the last name is > 20 chars, or if the 20 character
limit is in the middle of the first name?), then:

FieldString = (emp.LastName & "," & emp.FirstName & " " & emp.MI)
EmpList.Write(FieldString.PadRight(20).SubString(0, 20))
 
Back
Top