Метод подсчета количества токенов, соответствующих шаблону в строке. />
Входная строка может быть "$ 122 $$ dddd $ 1AASDS $$" < /code>. < /p>
Вывод метода должен быть 2 для строки выше. Лучший способ: < /p>
static int CalculateTokenCount()
{
string s = "$ab$$ask$$$$123$$";
int tokenCount = 0;
bool foundOneDollar = false;
bool foundSecondDollar = false;
if (string.IsNullOrEmpty(s))
{
return tokenCount;
}
for (int i = 0, x = 0; i < s.Length; i++)
{
if (s == '$' && !foundOneDollar)
{
foundOneDollar = true;
continue;
}
if (foundOneDollar)
{
if (s == '$' && !foundSecondDollar)
{
foundSecondDollar = true;
continue;
}
}
if (foundSecondDollar)
{
if (s == '$')
{
tokenCount++;
}
foundSecondDollar = false;
}
}
Console.WriteLine(tokenCount);
return tokenCount;
}
Подробнее здесь: https://stackoverflow.com/questions/155 ... n-a-string