在一段话里,统计某个字母的出现次数,使用Length属性和Substring()方法
还有统计某单词的出现次数,使用Length属性和IndexOf()方法

解决方案 »

  1.   

    用Linq吧 public int GetCount(string str,char s)
            {
                var col= str.ToCharArray().Where(p => p == s);
                return col.Count();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string s = "hhhhhfshamhsh";
                MessageBox.Show(GetCount(s,'h').ToString());
            }
      

  2.   

    编写Windows应用程序,在文本框textBox1中输入一个字符串,单击“统计”按钮,在label1中显示输出字母“e”的出现次数(不区分大小写)。
      

  3.   

    /// <summary>
            /// 
            /// </summary>
            /// <param name="str">你要验证的字符串</param>
            /// <param name="flag">你要找的字符串</param>
            /// <returns></returns>
            public int GetStrCount(string str, string flag)
            {
                int len = flag.Length;
                int total = str.Length;
                int count = 0;
                if (len>total)
                {
                    return 0;
                }
                for (int i = 0; i <= total-len; i++)
                {
                    if (str.Substring(i,len)==flag)
                    {
                        count++;
                    }
                }
                return count;
            }
      

  4.   

    单词也可以用LINQ啊var v = str.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries).Where(u => string.Compare(u, "某单词", true) == 0);
    return v.Count();
      

  5.   

    1.编写Windows应用程序,在文本框textBox1中输入一个字符串,单击“统计”按钮,在label1中显示输出字母“e”的出现次数(不区分大小写), 使用循环结构、Length属性和Substring()方法。
    2.编写Windows应用程序,在文本框textBox1中输入一个字符串,单击“统计”按钮,在label1中显示输出字符串“the”的出现次数(不区分大小写),使用循环结构、Length属性和IndexOf()方法。
      

  6.   

    靠,硬性指定用什么方法阿,那只好愚蠢一点:string str = "单词";
    int counter = 0;
    int len = 0;
    if (!String.IsNullOrWhiteSpace(textBox1.Text)) 
    {
       string inputString = textBox1.Text.Trim();
       int len = inputString.Length;
       int startIndex = 0;
       if (len >= str.Length)
       {
          while (startIndex <= len - str.Length && inputString.IndexOf(str, startIndex, StringComparison.CurrentCultureIgnoreCase) > -1)
          {
             counter++;
             startIndex = tIndex + str.Length;
          } 
       }
    }
    return counter;//所求
      

  7.   

    贴错了,应该是:string str = "单词";
    int counter = 0;
    int len = 0;
    if (!String.IsNullOrWhiteSpace(textBox1.Text)) 
    {
       string inputString = textBox1.Text.Trim();
       int len = inputString.Length;
       int startIndex = 0;
       if (len >= str.Length)
       {
          int tIndex = 0;
          while (startIndex <= len - str.Length && tIndex > -1)      {
             tIndex = inputString.IndexOf(str, startIndex, StringComparison.CurrentCultureIgnoreCase);
             if (tIndex > -1)
             {
                  counter++;
                  startIndex = tIndex + str.Length;
             }
          } 
       }
    }
    return counter;//所求
      

  8.   

    错误 1 “string”并不包含“IsNullOrWhiteSpace”的定义 E:\c#\ppt-45\ppt-45\Form1.cs 24 25 ppt-45
    错误 2 不能在此范围内声明名为“len”的局部变量,因为这样会使“len”具有不同的含义,而它已在“父级或当前”范围中表示其他内容了 E:\c#\ppt-45\ppt-45\Form1.cs 27 21 ppt-45
    错误 3 当前上下文中不存在名称“tIndex” E:\c#\ppt-45\ppt-45\Form1.cs 34 38 ppt-45
    错误 4 由于“ppt_45.Form1.button1_Click(object, System.EventArgs)”返回 void,返回关键字后面不得有对象表达式 E:\c#\ppt-45\ppt-45\Form1.cs 38 13 ppt-45
      

  9.   

    1.IsNullOrWhiteSpace是.net 4.0才有的,低版本把这句
    if (!String.IsNullOrWhiteSpace(textBox1.Text)) 
    改成
    if (!String.IsNullOrEmpty(textBox1.Text) && !String.IsNullOrEmpty(textBox1.Text.Trim())) 
    2.没看仔细 
    int len = inputString.Length;
    改成
    len = inputString.Length;3.上面已改4.你的方法是void,那么删除
    return counter;

    label1.Text = counter.ToString();
      

  10.   

    胡乱写的貌似行
    C#  string X;
            int i = 0;
            private void button1_Click(object sender, EventArgs e)
            {
                X = textBox1.Text.ToString();
                foreach (char y in X)
                { if (y == 'a') i++; }
                label1.Text = i.ToString();
            }
      

  11.   

    可以运行,无论是单个字母还是一个单词都可以,但我是初学者,看不大懂。为什么不用ToLower()或ToUpper()方法都可以统计?
      

  12.   


    新手。。没办法  不知道你是要用怎样的做 但只看你最初的要求的最表面意思。。好像这样真可能计算出某个字母的出现次数但后面你说 不分大小写  那把X = textBox1.Text.ToString();改成X = textBox1.Text.ToLower().ToString();把整个字符转成小写 然后再查。。不就能大小写也不管吗?。我入门级的。 刚只学到这些知识。。错了别见怪 我也跟着学习   对了 其实我这样做为什么不行呢?一样能统计出  我这样做会带来什么不好的结果?
      

  13.   

    分一下组。string str = "AaBcde张";
    var qeury = str.GroupBy(g => g.ToString().ToUpper())
                   .Select(s => new { key = s.Key, count = s.Count() });
      

  14.   

    //用于统计the出现的次数
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int[] a = new int[50];
                string str = textBox1.Text.ToLower ();
                int m = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    int n = str.IndexOf("the") +3;
                    if (n > i) m++;
                    str = str.Substring(n);
                }
                label1.Text = "the 出现的次数=" + m;
            }
        }
    }
      

  15.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication5
    {
       public partial class Form1 : Form
       {
           public Form1()
           {
               InitializeComponent();
           }
           private void button1_Click(object sender, EventArgs e)
           {
               int[] a = new int[30];
               string s = textBox1.Text.ToUpper();
               for (int i = 0; i < s.Length; i++)
               {int s1 = char.Parse(s.Substring(i, 1)) - 'A';
               if (s1 == 4) a[s1]++;
               }
               label1.Text += "字母e出现的次数" + "=" + a[4] ;
           }
       }
    }