检查某个字符是否后跟C#中的另一个字符

时间:2014-05-23 13:56:03

标签: c# regex string

我有string format = #,##,0.00,,,我需要计算其中有多少逗号,但在前面的例子中,对我来说唯一相关的逗号是最后两个。

我想检查逗号是否后跟"#""0"

这样的解决方案会起作用吗?

int count = 0
for (int i = format.IndexOf(','); i > -1; i = format.IndexOf(',', i + 1))
{
    // for loop end when i=-1 (',' not found)
       // if next character is # or 0 don't count
       // else count
}

8 个答案:

答案 0 :(得分:1)

如果你想计算未被"#"或" 0",无论文本位于何处(开始,结束),您都可以这样做:

Regex.Matches("#,##,0.00,,#,##,0.00,,", ",(?![#0])").Count

返回三个。请注意,第三个逗号后面没有#或0.如果您希望逗号只在它们位于字符串末尾时匹配:

Regex.Matches("#,##,0.00,,#,##,0.00,,", ",(?![#0]),*$").Count

返回两个。

答案 1 :(得分:0)

    private int GetCount()
    {
        string format = "#,##,0.00,,";

        int count = 0;
        for (int i = 0; i < format.Length; i++)
        {
            //if current char is not ',' skip it
            if (!format[i].Equals(','))
            {
                continue;
            }

            //if current char is ',' and its last in string => count++ because no '#' or '0' folows it
            if ((i + 1) == format.Length)
            {
                count++;
                break;
            }

            //if '0' or '#' folows current char, skip current char
            if (format[i + 1].Equals('#')
                || format[i + 1].Equals('0'))
            {
                continue;
            }
            //next char is not '0' or '#' => count++
            count++;
        }
        return count;
    }

答案 2 :(得分:0)

你可以试试这个:

  string format = @"#,##,0.00,,";
  string[] arrStr = format.Split(',');
  int count = 0;

  for(int i = 1; i < arrStr.length - 1; i++) 
  {
       string s = arrStr[i];
       //ignore string starting with # or 0 
       if (!s.StartsWith("#") && !s.StartsWith("0")) 
       {
            count++;
       }
   }

以下是使用上述代码的不同输入和输出:

 //string format = @"#,##,0.00,,";  
 //Commas Count = 2

 //string format = @"a,a,#,0,a,,";  
 //Commas Count = 4

 //string format = @",,";  
 //Commas Count = 2

 //string format = @",";  
 //Commas Count = 1

 //string format = @",#";  
 //Commas Count = 0

答案 3 :(得分:0)

使用正则表达式。下面的模式仅解决了您提到的“最后一个逗号”方案。

string format = "#,##,0.00,,";

string pattern = @"^#,##,\d\.\d\d(?<LastCommas>,+)$";

var myRegex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture);

Match match = myRegex.Match(format);

GroupCollection capturedGroups = match.Groups;

// This will get you the number of commas
int count = capturedGroups["LastCommas"].Value.Length;

如果“最后一个逗号”是可选的(即可以是零或更多),请将上面的pattern替换为:

string pattern = @"^#,##,\d\.\d\d(?<LastCommas>,*)$";

答案 4 :(得分:0)

    public static int CountDelimiter(string data)
    {
        var count = 0;
        var previous = char.MinValue;
        foreach (var c in data)
        {
            if (previous != '#' && preivous !='0' && c == ',')
                count++;
            previous = c;
        }
        return count;
    }

或者如果你想在单一命令样式中使用它...你可以这样做:

    public static int CountDelimiter(string data)
    {
        return data.Where((x, xi) => x == ',' && 
        (xi == 0 || (xi > 0 && data[xi - 1] != '0' && data[xi - 1] != '#'))).Count();
    }

答案 5 :(得分:0)

string string1 = "#,##,$,$%%$,0,^^^,,";

char[] a = new char[1]
{
    ','
};

string[] strArray = string1.Split(a);

foreach (string s in strArray)
        {
            if (s.StartsWith("0") || s.StartsWith("#") || s.StartsWith(",") || s == string.Empty)
            {
                count++;
            }
        }

这里有两个逗号,循环将检查string.Empty

答案 6 :(得分:0)

我不确定你要做什么,但可能是这样:

static int X(string str)
{
    int endPos = str.Length - 1;
    int count = 0;

    for (int pos = 0; pos <= endPos; pos++)
    {
        if (str[pos] == ',')
        {
            if (pos < endPos)
            {
                char next = str[pos + 1];

                if (next == '#' || next == '0')
                {
                    pos++;

                    continue;
                }
            }

            count++;
        }
    }

    return count;
}

答案 7 :(得分:0)

我没有对此进行测试,但确实编译了:

public static int GetModifiedCommaCount(string searchString)
{
    int result = 0;
    int lastIndex = searchString.Length - 1;

    // start looping at our first match to save time
    for (int i = searchString.IndexOf(','); i <= lastIndex && i > 0; i++)
    {
        if (searchString[i] == ',' && (i >= lastIndex || (searchString[i + 1] != '0' && searchString[i + 1] != '#')))
        {
            result++;
        }
    }

    return result;
}