在字符串里寻找某字符出现的个数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 字符串1
{
    class Program
    {
        static void Main(string[] args) 
        {
            int n = 0; 

            Console.WriteLine("请输入字符串:"); 

            string B = Console.ReadLine();     

            B = B.ToLower();   
              
            if (B.IndexOf("b") < 0)       
            {
                Console.WriteLine("您输入的字符串里不包含字母b");

            }
            else
            {

                int c = B.Length;

                for ( n = 0; n <= c; n++)
                {
                    int b = B.IndexOf("b");
                    if (b < 0)
                                          
                        break;             
       
                    B = B.Substring(b+1);

                } Console.WriteLine("字符串中含b有" +n+"个");
                
            }Console.ReadLine();
            

        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 作业4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入字符串:");

            string A = Console.ReadLine();
            A = A.ToLower();

            if (A.IndexOf("b") < 0)
            {
                Console.WriteLine("您输入的字符串里不包含b");
            }
            else
            {
                int C = A.Length;

                A = A.Replace("b", "");

                int Q = A.Length;

                int w = C - Q;
                Console.WriteLine("您输入的字符串中有"+w+"个b");


            } Console.ReadLine();
        }
    }
}

  把b替换成无字符    俩长度相减

原文地址:https://www.cnblogs.com/hanke123/p/4709357.html