小编sam*_*ali的帖子

计算字符串中的数字(从0到9)

我想计算从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)

c# count

2
推荐指数
1
解决办法
3294
查看次数

FileStream Write() 方法只写入未知符号

这是我的代码,它只在 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 和其他一些符号。

c# filestream

1
推荐指数
1
解决办法
1045
查看次数

如何只在字符串的第一句中找到一个字母的出现次数?

我想在第一句中找到字母"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)

c# string char

0
推荐指数
1
解决办法
1390
查看次数

标签 统计

c# ×3

char ×1

count ×1

filestream ×1

string ×1