|
# region Heading
/**************************************************************************************************************/
/* */
/* LibExt.Untabify.cs */
/* */
/* Extension method that untabifies the leading whitespace of a string */
/* */
/* This is free code, use it as you require. If you modify it please use your own namespace. */
/* */
/* If you like it or have suggestions for improvements please let me know at: PIEBALDconsult@aol.com */
/* */
/* Modification history: */
/* 2009-01-13 Sir John E. Boucher Created */
/* */
/**************************************************************************************************************/
# endregion
namespace PIEBALD.Lib.LibExt.Untabify
{
/**
<summary>
Extension method that untabifies the leading whitespace of a string.
</summary>
*/
public static partial class LibExt
{
/**
<summary>
Extension method that untabifies the leading whitespace of a string.
String contents after the first non-whitespace character remain unchanged.
</summary>
<param name="String">
The string to untabify.
</param>
<param name="SpacesPerIndent">
The number of SPACEs that represent an indent.
Zero (0) results in the removal of any TABs.
</param>
*/
public static string
Untabify
(
this string String
,
byte SpacesPerIndent
)
{
/* Let it throw the Exception */
System.Text.StringBuilder result = new System.Text.StringBuilder ( String.Length ) ;
int offset = 0 ;
while ( offset < String.Length )
{
if ( String [ offset ] == ' ' )
{
result.Append ( ' ' ) ;
offset++ ;
}
else
{
if ( String [ offset ] == '\t' )
{
if ( SpacesPerIndent > 0 )
{
for ( int i = result.Length % SpacesPerIndent ; i < SpacesPerIndent ; i++ )
{
result.Append ( ' ' ) ;
}
}
offset++ ;
}
else
{
break ;
}
}
}
result.Append ( String.Substring ( offset ) ) ;
return ( result.ToString() ) ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.