可以使用 Count 方法来判断字符串中包含多少个指定字符。

以下是一个示例代码,用于判断字符串中包含多少个 '/' 字符。

using System;

class Program
{
    static void Main(string[] args)
    {
        string str = 'C:/Program Files/Microsoft';
        
        int count = str.Count(c => c == '/');
        
        Console.WriteLine('字符串中包含 {0} 个 '/' 字符。', count);
    }
}

输出结果为:

字符串中包含 2 个 '/' 字符。
C# 统计字符串中'/'字符数量

原文地址: https://www.cveoy.top/t/topic/g7vh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录