We help IT Professionals succeed at work.

Insert Tab into VB.NET String

18,552 Views
Last Modified: 2013-11-07
I need to insert spaces/Tabs into a string so that certain items will line up into columns displayed.  For example, here is what my strings currently look like when displayed:

John Smith Asheville,NC
Sally Rebecca Johnson Pittsburgh,PA

And here is how I would like my strings to look:

John Smith                                  Asheville,NC
Sally Rebecca Johnson               Pittsburgh,PA


Here is some code that I've tried with no success:

'** Try #1
Dim thePerson as String = theName & "              " & theCity
Response.write(thePerson)

'** Try #2
Dim theLength as Integer = len(theName)
Dim i as integer = 0

For i = 0 to (50-theLength)

    theName = theName & " "
    '** Also Tried: theName = theName & Chr(32)
   
Next

thePerson = theName & theLocation '  Note: Result is still "John Smith Asheville NC"
Comment
Watch Question

David H.H.LeeDigital Marketing (Hospitality)
CERTIFIED EXPERT

Commented:
Dear Shiseiryu1,
Try this instead:
eg:
str = "John Smith" & ReturnTab(50) & "Asheville NC"

Tab Function:
Function ReturnTab(intTotal as integer) as String
  Return Replace("".ToString.PadLeft(intTotal , " ")," "," ")
End Function

Or amend your previous code, just add " " for space.
eg:
theName = theName & " "
If this is going to display on the web, try this character: 	 .  You can also try putting \t in place of the tab.

hth

valkyrie_nc
CERTIFIED EXPERT
Top Expert 2006

Commented:
Dim thePerson as String = theName.PadRight(50, " ") & theCity
Response.write(thePerson)
I always use a table to line things up
It won't look like a table, but it will do the trick.
It's a little more typing, but very versatile especially when you want to then change the column width.
' Here's a crude example

Public Sub DoThang()
      Dim strName1 As String = "John Smith"
      Dim strAddr1 As String = "Asheville, NC"
      Dim strName2 As String = "Sally Rebecca Johnson"
      Dim strAddr2 As String = "Pittsburgh, PA"

      Response.Write("<TABLE WIDTH='50%' BORDER='0'>")
      Response.Write("<TR><TD>Name</TD><TD>Address</TD></TR>") ' use <TH> for formal table header instead of <TD>
      Response.Write("<TR><TD>" + strName1 + "</TD><TD>" + strAddr1 + "</TD></TR>")
      Response.Write("<TR><TD>" + strName2 + "</TD><TD>" + strAddr2 + "</TD></TR>")
      Response.Write("</TABLE>")
   End Sub

Author

Commented:
Thank you all for your quick responses and feedback.  Please allow me to elaborate on why I need this solution:  I'm using an AJAX component which as you type a person's name in a textbox, suggestions appear below the textbox.  You can then push the down key to select any of the suggestions.  As you go through the suggestions, the text in the textbox is populated with the value.

x_com: You solution is the closest to what I need.  It puts spaces between the name and the city.  However, even though I'm putting (50 - len(theName)) spaces between the name and the city, everything is still not lining up exactly.  I think this may be because although I have the same number of characters between the name and city, the width of each name varies based on the characters in it.  (ex: "Illian" takes up less space on the screen then "Chadly").  The other issue with your solution is that when you hover of the suggestion and the textbox is filled with the suggestion, the textbox shows the &nbsp;.  

valkyrie_nc: I tried &#09; instead of &nbsp; but it didn't seem to work.  :(  This is a web-based problem so I don't think the "/t" is applicable.

amit_g: You suggestion didn't create any spaces at all.  It looks like it should..but it isn't.  For some reason the system is trimming the spaces.  :(

Triskelion: because the string is used to populate an AJAX component, I can't use tables in it.


Thank you all again for your help...if you have any other suggestions I would be most grateful.
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.