2

私は以下を参照してくださいプログラムを持っています

メソッドを作成しましたが、console.writeline(str.length) のような簡単な方法ではなく、コンソールに表示したいです。作った方法を使いたい。

誰か助けてください

前もって感謝します

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "dit is een test 1,2,3";
            Console.WriteLine(str);
        }

        public int CountAllNumbersAndChar(string str) 
        { 
            return str.Length; 
        }

    }
}

アップデート:

私は今、次のプログラムを持っています

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "this is a test 1,2,3";
            int length = CountAllNumbersAndChar(str);
            Console.WriteLine(str);
            Console.WriteLine(length);// met de methode maar kan handiger met onderstaand voor beeld
      // Console.WriteLine(str.Length);
       // int numbers = str.Count(Char.IsNumber); // de makelijkste makelijke manier 
        //Console.WriteLine(numbers);
        int countnumber = CountNumbers(str) ;
        Console.WriteLine(countnumber);
        int countwords = words(str);
        Console.WriteLine(countwords);


    }

    public static int CountAllNumbersAndChar(string str)
    {
        return str.Length;
    }
    public static int CountNumbers(string str)
    {
        return str.Count(Char.IsNumber);
    }
    public static int words(string str)
    {
        int words = str.Split().Count(str => str.All(Char.IsLetter));
    }
}

}

しかし、それはまだうまくいきません。

4

4 に答える 4

4

これは、あなたの望むことですか?

Console.WriteLine(CountAllNumbersAndChar(str));
于 2009-09-26T08:13:04.803 に答える
2

これがあなたのやり方です。 以下のコードに注意してください。として宣言しないと、から電話をかけることはできません。public static int CountAllNumbersAndChar(string str)CountAllNumbersAndCharMainstatic

using System; 
using System.Collections.Generic; 
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "this is a test 1,2,3";
            int length = CountAllNumbersAndChar(str);    

            Console.WriteLine(length);
        }

        public static int CountAllNumbersAndChar(string str) 
        { 
            return str.Length; 
        }
    }
}
于 2009-09-26T08:15:21.267 に答える
0

これらすべてのタスクに LINQ を使用できます。あなたがそれに精通しているかどうかはわかりませんが。非常に単純ですが、コードを見て、従うことができるかどうかを確認してください。

string str = "dit is een test 1,2,3";

// Length of the string
int chars = str.Length;

// LINQ: Count all characters that is a number
int numbers = str.Count(Char.IsNumber);

// LINQ: Split the string on whitespace and count the 
// elements that contains only letters
int words = str.Split().Count(s => s.All(Char.IsLetter));

Console.WriteLine(chars); // -> 21
Console.WriteLine(numbers); // -> 3
Console.WriteLine(words); // -> 4

もちろん、私が単語を数えている方法は完璧ではありませんが、それはあなたが始めるのに役立つはずです. より正確な方法については、何百もの例があるため、Google で検索する必要があります。

于 2009-09-26T09:00:03.940 に答える
0

文字列内の数字の数を数えたいと思います

      public int CountAllNumbersAndChar(string str)         
       {
           return  str.Split(new char[]{' ',','},
         StringSplitOptions.RemoveEmptyEntries).Count    
         (
           x=>
           {
               int d;
               return int.TryParse(x,out d);
           }
        );   
       }
于 2009-09-26T09:19:18.410 に答える