limiting number of characters - pad

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!!!
 
H

Hal Rosser

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)
 
J

Jack Jackson

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top