我想计算从0到9的数字是多少个字符串.尝试了一些代码,但它不起作用,每次都返回0.什么是错的,如何解决?如果你能告诉我如何使用srting.Count()方法.谢谢.
// Attempt 1
string str = textBox1.Text;
int b = 0;
int n = 0;
foreach (char a in str)
{
if ((b > 0) && (b < 9))
{
if ((char)b == a)
n++;
}
}
label1.Text = n;
// Attempt 2
string str = textBox1.Text;
int n = 0;
foreach (char a in str)
{
int[] k = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (int b in k)
{
if …Run Code Online (Sandbox Code Playgroud) 这是我的代码,它只在 fileout.txt 中写入这些符号。出了什么问题,如何解决?
byte[] bArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
FileStream file_out = new FileStream(@"C:\fileout.txt",
FileMode.Create);
file_out.Write(bArray, 2, 4);
file_out.Close();
Run Code Online (Sandbox Code Playgroud)
抱歉,这里没有显示符号,但为什么我不知道。这个符号有点像 upside 和 small r 和其他一些符号。
我想在第一句中找到字母"a"的数量.下面的代码在所有句子中都找到了"a",但我只想要第一句话.
static void Main(string[] args)
{
string text; int k = 0;
text = "bla bla bla. something second. maybe last sentence.";
foreach (char a in text)
{
char b = 'a';
if (b == a)
{
k += 1;
}
}
Console.WriteLine("number of a in first sentence is " + k);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)