-1

I need to count the number of tabs between words in the string

string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS";


int TabsDateVersion;     //number of tabs between "Date" and "Version"
int TabsVersionAuthor;   //number of tabs between "Version" and "Author"
int tabsAuthorComments;  //number of tabs between "Author" and "Comments"

Note: words are only separated by one or more tabs.

Any idea how to do this in C#, so I have 3 different counts?

BertAR
  • 425
  • 3
  • 18
  • 1
    can i ask why u need to count the tabs? - if u want for example a textfile where the words are formatted like a table, there are more simplier solutions – Raizzen Jan 10 '18 at 07:19
  • Are they actually tab characters (`\t`) or are they space characters like in your example? – Ian H. Jan 10 '18 at 07:19
  • @IanH. They are only tab characters between the words – BertAR Jan 10 '18 at 07:21
  • do you know how to use the function [Split](https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx) ? you would simply need to count the number of characters in each position of the returned array. But you really should consider to post at least one own attempt to solve this problem. Have you tried at least using a for-loop? – Mong Zhu Jan 10 '18 at 07:24
  • @Raizzen. It is from an XML-file and I am stuck to it – BertAR Jan 10 '18 at 07:30
  • What output do you expect? The total number of tabs (10) or an array of the count between words in a string (3, 2, 5)? – Middas Jan 10 '18 at 07:30
  • @Middas array of the count between words – BertAR Jan 10 '18 at 07:48

4 Answers4

1

If the string HAS tabs (and not 4 spaces rapresenting the tab) you can easily count using LINQ

var s = "DATE,      VERSION             AUTHOR      COMMENTS";

Console.WriteLine(CountTabs(s,"DATE","VERSION"));
Console.WriteLine(CountTabs(s,"VERSION","AUTHOR"));
Console.WriteLine(CountTabs(s,"AUTHOR","COMMENTS"));

public static int CountTabs(string source, string fromVal, string toVal)
{
    int startIdx = source.IndexOf(fromVal);
    int endIdx = source.IndexOf(toVal);

    return source.Skip(startIdx).Take(endIdx - startIdx).Count(c => c == '\t');
}

Note: No error handling if fromVal and toVal is not part of source.

Michael Mairegger
  • 6,833
  • 28
  • 41
1

Using information on how to get text between words from this answer, I wrote a simple function that takes the total string, the left word, the right word and the char to look for.

public int CountCharactersBetweenWords(string totalString, string leftWord, string rightWord, char search)
{
    // Find the indexes of the end of the left and the start of the right word
    int pFrom = totalString.IndexOf(leftWord) + leftWord.Length;
    int pTo = totalString.LastIndexOf(rightWord);

    // Get the substring between the words
    string between = totalString.Substring(pFrom, pTo - pFrom);

    // Count the characters that are equal with the character to search for
    return between.Count(c => c == search);
}

Call it like this:

string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS";
int count = CountCharactersBetweenWords(s, "DATE", "VERSION", '\t');
Ian H.
  • 3,840
  • 4
  • 30
  • 60
1

Split the string by keywords then count it.

 string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\tCOMMENTS";
 string[] list = s.Split(new string[] { "DATE", "VERSION", "COMMENTS", "AUTHOR" }, StringSplitOptions.None);
 int TabsDateVersion = list[1].Count(x => x == '\t');     
 int TabsVersionAuthor = list[2].Count(x => x == '\t');
 int tabsAuthorComments = list[3].Count(x => x == '\t');
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
0

Let's try a one liner! With regex!

Regex.Match("DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS", 
    "DATE(\\t*)").Groups[1].Length

The regex used here is

DATE(\t*)

It basically looks for "DATE" and captures all the tab characters after it and put them all in group 1. You can replace "DATE" with "VERSION", "COMMENTS" etc.

Remember to add a using directive to System.Text.RegularExpressions!

Sweeper
  • 213,210
  • 22
  • 193
  • 313