Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I have a problem with "GenerateCharacterCount"...in windows form, not in console.
E.x I have a text

string ="Hello World";

label1.text="character count: " + GenerateCharacterCoun(data).Tostring();


but in windows for "GenerateCharacterCount" is unknown, does anyone can tell we where is a problem.... shuld I add any reference or what. !


Thnx !
Posted
Updated 18-Apr-12 22:01pm
v2

It might help,
C#
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string dataToTest="Hello World<>@@@$$$##555!!!";
            Console.WriteLine("Total Characters: {0}",dataToTest.Where(ch => Char.IsLetter(ch)).Count());
            Console.WriteLine("Total Length: {0}", dataToTest.Length);
        }
    }
}


:)
 
Share this answer
 
Comments
h7h7h7 19-Apr-12 4:14am    
thnx Mohammad !
VJ Reddy 19-Apr-12 19:59pm    
Succinct with LINQ. 5!
Mohammad A Rahman 19-Apr-12 20:02pm    
Thank you very much VJ :)
try the below one in windows forms


string strsample = "Hello World";
label1.Text = "character count: " + strsample.Length.ToString();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Apr-12 3:26am    
What OP is asking about? About characters. Not letters or something which would need classification. Formally, a correct answer, my 5.
--SA
h7h7h7 19-Apr-12 4:09am    
Yes it was a correct answer
Thankyou!
Sergey Alexandrovich Kryukov 20-Apr-12 15:41pm    
Of course it is. You are more than welcome.
--SA
Hi,

You can use Length property of string to count characters:
C#
label1.text="character count: " + data.Length.ToString();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Apr-12 3:27am    
What OP is asking about? About characters. Not letters or something which would need classification. Formally, a correct answer, my 5.
--SA
h7h7h7 19-Apr-12 4:09am    
Thankyou...! :)
GenerateCharacterCount is not .Net method. I guess you have copied it from here:
Parsing Sentences and Building Text Statics in C#[^]
Correct?
If you want to use it, just copy the function into your code:
C#
public int GenerateCharacterCount(string allText)
        {
            int rtn = 0;

            // clean up the string by
            // removing newlines and by trimming
            // both ends
            string sTemp = allText;
            sTemp = sTemp.Replace(Environment.NewLine, string.Empty);
            sTemp = sTemp.Trim();

            // split the string into sentences
            // using a regular expression
            string[] splitSentences =
                Regex.Split(sTemp,
                    @"(?<=['""A-Za-z0-9][\.\!\?])\s+(?=[A-Z])");

            // loop through the sentences to get character counts
            for(int cnt=0; cnt<splitSentences.Length; cnt++)
            {
                // get the current sentence
                string sSentence = splitSentences[cnt].ToString();

                // trim it
                sSentence = sSentence.Trim();

                // convert it to a character array
                char[] sentence = sSentence.ToCharArray();

                // test each character and
                // add it to the return value
                // if it passes
                for (int i = 0; i < sentence.Length; i++)
                {
                    // make sure it is a letter, number,
                    // punctuation or whitespace before
                    // adding it to the tally
                    if (char.IsLetterOrDigit(sentence[i]) ||
                        char.IsPunctuation(sentence[i]) ||
                        char.IsWhiteSpace(sentence[i]))
                        rtn += 1;
                }
            }

            // return the final tally
            return rtn;
        }
 
Share this answer
 
for characters it work
C#
string strsample = "Hello World";
label1.Text = "character count: " + strsample.Length.ToString();



but if I want to count words what should I do....!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900