用c#在屏幕上输入一个字符串,求每个字符串出现的次数
1个回答
展开全部
ing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Demo{ class Program { static void Main(string[] args) { string motherString = "qwertyuiopasdfghjklqwezxcqwevbnqwem"; string childString = "qwe"; var num = Count(motherString,childString); Console.WriteLine("{0}含有的{1}个数为:{2}",motherString,childString,num); Console.Read(); } static int Count(string WithinString, string search) { if (string.IsNullOrEmpty(search)) throw new ArgumentNullException("search"); int counter = 0; int index = WithinString.IndexOf(search, 0); while (index >= 0 && index < WithinString.Length) { counter++; index = WithinString.IndexOf(search, index + search.Length); } return counter; }//这是一个方法,WithinString字符串,search你想查看个数的字符串,counter返回个数 }}
namespace Demo{ class Program { static void Main(string[] args) { string motherString = "qwertyuiopasdfghjklqwezxcqwevbnqwem"; string childString = "qwe"; var num = Count(motherString,childString); Console.WriteLine("{0}含有的{1}个数为:{2}",motherString,childString,num); Console.Read(); } static int Count(string WithinString, string search) { if (string.IsNullOrEmpty(search)) throw new ArgumentNullException("search"); int counter = 0; int index = WithinString.IndexOf(search, 0); while (index >= 0 && index < WithinString.Length) { counter++; index = WithinString.IndexOf(search, index + search.Length); } return counter; }//这是一个方法,WithinString字符串,search你想查看个数的字符串,counter返回个数 }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询