-3

I have a string which have 3 '\t's and when I use the proper methods like:

string.padright(totalWidth);

Or

string.format("{0,width}",myText);

Or even a function from scratch, I get a problem with that '\t' escape in the string which counts one but depend on a string, it's between 0 to 8.

At the end, if I have this string "blah\tblah\tblah" and I apply those methods to have a 20 length string I get this

"blah    blah    blah      "

which has a length of 30.

How can I count the spaces that '\t' fills after the string is displayed?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    Please provide a [mcve]. – mjwills Jun 12 '19 at 11:26
  • Am I understanding you correctly that you simply want to know how many `\t` are in a given string? Also please take a look at [mcve] – LocEngineer Jun 12 '19 at 11:30
  • I can get the numbers of \t but some times \t lenght is 1. Some times is 2 and so on. I wand the total lenght thay produce – Difender Amini Jun 12 '19 at 11:32
  • `\t` is a tab character which has a length of 1, what exactly are you trying to do here? – DavidG Jun 12 '19 at 11:32
  • Hay they have lenght 1 in code but when they go to output window they lenght is difrent – Difender Amini Jun 12 '19 at 11:34
  • The tab character itself is a single character, how many characters it takes when displayed is up to the app displaying it. A tab will move the cursor to the next tab stop. In a console, this is probably every 8 characters starting a column zero. – DavidG Jun 12 '19 at 11:41
  • Maybe this will help: https://stackoverflow.com/questions/14167033/visual-studio-replace-tab-with-4-spaces/14167067 . There you can a) determine the amount of space a tab takes and b) choose to replace tabs with spaces if you wish so. – LocEngineer Jun 12 '19 at 11:48

1 Answers1

-1

Tabs are a bit strange because they're a single character, but the effective width is variable. It depends on how many characters are on screen currently, and the tab width.

It's possible to write a function that expands the tab characters to spaces.

string ExpandTabs(string input, int tabWidth = 8)
{
    if (string.IsNullOrEmpty(input))
        return input;

    var result = new System.Text.StringBuilder(input.Length);

    foreach (var ch in input)
    {
        if (ch == '\t')
        {
            result.Append(' ');
            while (result.Length % tabWidth != 0)
                result.Append(' ');
        }
        else
            result.Append(ch);
    }

    return result.ToString();
}

Note that this is a fairly naive function. It won't work properly if there are embedded \r or \n characters, and other characters could also cause problems.

NZgeek
  • 199
  • 4
  • There's a whole bunch of things wrong with this function. Among other issues, you are assuming the output is starting on column 0 and the output doesn't flow over multiple lines. – DavidG Jun 12 '19 at 11:45
  • Thanks that's enough for me – Difender Amini Jun 12 '19 at 11:49
  • @DavidG I did note at the bottom that it's a naive function. It's not supposed to be comprehensive or to cover all possible use cases. – NZgeek Jun 14 '19 at 10:02