Filip Nilsson
Filip Nilsson

Reputation: 407

Separate code in ASP.NET

Dear Stackoverflowers,

I am trying to build a simple web application in ASP.NET where I have a textbox and a button and when the button is pressed I should count the number of UPPERCASE-characters. I was thinking of trying to use a RegularExpression but i dont know how to separate it.

Now i have a .cs file that looks like this:

public static class TextAnalyzer
{
    public static int GetNumberOfCapitals(string text)
    {

    }
}

And a aspx.cs file that looks like this and is connected to my button.

protected void Button1_Click(object sender, EventArgs e)
    {
     //   Regex caps = new Regex("");
    }

Where do i put the regular expression? in aspx.cs or .cs? And at the samt time you can give me some other tips, im an absolute beginner and this is my first web page! Thanks in advance.

Upvotes: 0

Views: 145

Answers (4)

Huske
Huske

Reputation: 9296

Here is sample console application which you can adapt for you web needs:

static void Main()
{
    Regex capitalLetters = new Regex("[A-Z]");
    string sampleString = "This is NOT a HellO WoRlD sample Applicati0N. OK?";

    var matches = capitalLetters.Matches(sampleString);

    Console.WriteLine("Matches count: {0}", matches.Count);

    Console.WriteLine("Press ENTER");
    Console.ReadLine();
}

This uses regular expression to count the number of upper case characters

Upvotes: 0

Icarus
Icarus

Reputation: 63966

Something like this:

var NumOfUpper = (from c in text.ToCharArray()
                 where Char.IsUpper(c)
                 select c).ToList().Count();

Upvotes: 2

Abbas
Abbas

Reputation: 14432

Your code in the button-click event could look like this:

int capitals = TextAnalyzer.GetNumberOfCapitals(textBox1.Text);
//do something with 'capitals'

The code in the event could be like this:

public static int GetNumberOfCapitals(string text) 
{ 
    return text.ToArray().Count(c => c.ToString() == c.ToString().ToUpper());
} 

Upvotes: 1

Antony Scott
Antony Scott

Reputation: 21998

Why not call .ToCharArray on the string and use a bit of linq to count all characters where .IsUpper is true.

Upvotes: 3

Related Questions