在一段话里,统计某个字母的出现次数,使用Length属性和Substring()方法
还有统计某单词的出现次数,使用Length属性和IndexOf()方法
还有统计某单词的出现次数,使用Length属性和IndexOf()方法
解决方案 »
- 想写个工具将excel批量的简转繁,有什么思路么
- 关于文件夹移除
- ACC中全数字字符串排序怎么写SQL语句
- 问个数据库的
- c#中关于控件数组的事件响应问题
- datatable的select问题!
- 请问dataset中如何添加一列?比如第一列数据,第二列要添加一个按钮
- 为什么我用了DataSet 以后,DataGridTextBoxColumn 的HeaderText的中文就无法显示了
- 欢迎大家来讨论一下在.Net的架构实现中怎样工厂模式的使用?
- 有人在用c#开发ASPX过程中用过DATALIST控件么?
- c# indexer的优点?
- wsdl方式添加webservices后,如何加cookie
{
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());
}
///
/// </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;
}
return v.Count();
2.编写Windows应用程序,在文本框textBox1中输入一个字符串,单击“统计”按钮,在label1中显示输出字符串“the”的出现次数(不区分大小写),使用循环结构、Length属性和IndexOf()方法。
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;//所求
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;//所求
错误 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
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();
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();
}
新手。。没办法 不知道你是要用怎样的做 但只看你最初的要求的最表面意思。。好像这样真可能计算出某个字母的出现次数但后面你说 不分大小写 那把X = textBox1.Text.ToString();改成X = textBox1.Text.ToLower().ToString();把整个字符转成小写 然后再查。。不就能大小写也不管吗?。我入门级的。 刚只学到这些知识。。错了别见怪 我也跟着学习 对了 其实我这样做为什么不行呢?一样能统计出 我这样做会带来什么不好的结果?
var qeury = str.GroupBy(g => g.ToString().ToUpper())
.Select(s => new { key = s.Key, count = s.Count() });
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;
}
}
}
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] ;
}
}
}