Recipe 5.30. Converting Tabs to Spaces


Problem

You need to convert a string's tab characters to spaces while preserving the string's spacing.

Solution

Sample code folder: Chapter 05\TabsToSpaces

Create a function to convert tabs to spaces in the defined way:

 Public Function TabsToSpaces(ByVal source As String, _       ByVal tabSize As Integer) As String    ' ----- Replace tabs with space characters.    Dim result As New System.Text.StringBuilder    Dim counter As Integer    For counter = 0 To source.Length - 1       If (source.Chars(counter) = vbTab) Then          Do             result.Append(Space(1))          Loop Until ((result.Length Mod   tabSize) = 0)       Else          result.Append(source.Chars(counter))       End If    Next counter    Return result.ToString( ) End Function 

Discussion

The trick to replacing the tabs is to insert just the right number of spaces to preserve the original alignment of the text. Tab characters generally shift the next character to a position that is an exact multiple of the tab spacing. In Visual Studio, this spacing constant is often 4, but in many text editors, and even in the Windows Forms TextBox control, the standard tab spacing is 8. The sample function accepts an argument to set the tab-spacing constant to any value.

The function uses a StringBuilder to rebuild the original string, replacing tabs with enough spaces to maintain the alignment. The Chars property of the string makes it easy to access and process each individual character from the string, and the Mod() function simplifies the math checks required to determine the number of spaces to insert.

This code shows the TabsToSpaces() function in use:

 Dim tabs As String = _    "This~is~~a~tabbed~~~string".Replace("~"c, vbTab) Dim spaces As String = TabsToSpaces(tabs, 8) Dim periods As String = spaces.Replace(" "c, "."c) 

The first line builds a string comprised of words separated by multiple tab characters. The tilde (~) characters provide a visual way to see where the tabs will go, and the Replace() method replaces each tilde with a tab.

The second statement calls the new function and places the returned string in spaces. This string contains no tab characters, but it does contain many spaces between the words.

The periods string provides a visual way to see the spaces more clearly. The Replace() method in this case replaces each space with a period.

Figure 5-34 shows these three strings displayed on a form containing three TextBox controls. Setting the Font property to Courier New, a fixed-width font, more clearly shows the alignment of the characters in the strings. The tab-spacing constant in these text boxes is 8, which is the value passed to TabsToSpaces(), correctly replacing the tabs and maintaining the original alignment.

See Also

Recipe 5.16 also discusses replacing substrings.

Figure 5-34. The same string with tabs, spaces instead of tabs, and periods instead of spaces





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net