How do I show results in C #?

I have the program below

I made this method, but I want to display it in the console and not in a simple way like console.writeline (str.length). I want to use the method I made.

can someone help me please

early

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; 
        }

    }
}

      

Update:

Now I have the following program

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));
    }
}

      

}

but it still doesn't work can someone tell me what should i change?

+2


source to share


4 answers


Is this what you want?



Console.WriteLine(CountAllNumbersAndChar(str));

      

+4


source


This is how you do it. Note in the code below. You cannot call from unless you declare it as .public

static

int CountAllNumbersAndChar(string str)

CountAllNumbersAndChar

Main

static



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; 
        }
    }
}

      

+2


source


You can use LINQ for all these tasks. Although I'm not sure if you are familiar with this. It's really simple, so take a look at the code and see if you can follow along.

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

      

Of course, the way I count words is not perfect, but it should get you started. For more accurate methods, you should use Google as there are hundreds of examples.

0


source


I think you want to count the number of numbers inside your string

      public int CountAllNumbersAndChar(string str)         
       {
           return  str.Split(new char[]{' ',','},
         StringSplitOptions.RemoveEmptyEntries).Count    
         (
           x=>
           {
               int d;
               return int.TryParse(x,out d);
           }
        );   
       }

      

0


source







All Articles