By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

How to count special characters?

Ajay Bhalala
100+
P: 119
Hello experts,

I have 1 form.

In this form there is one textbox named "txtName" and 1 button named"btnCountChar"

I want to count the special characters entered in the textbox. How can I do this? Please help me. It's urgent.

Thank you in advance.
Jan 13 '15 #1

✓ answered by Frinavale

Have you learned about using Regular Expressions?

They are amazingly powerful...you describe what you are looking for based on a pattern and then ask the regular expression if anything in your input matches that pattern.

The regular expression pattern syntax can get tricky sometimes...but your pattern would be really easy.

Your pattern would be: are there any characters in my input that match "This Set Of Special Characters".

If your "special characters" are just punctuation they would include:
Expand|Select|Wrap|Line Numbers
  1. !"#$%&'()*+,-./:;<=>?@[\]^_`{|}
Some of those special characters are used in regular expression pattern syntax or they represent wild cards in regular expressions and so you need to negate them using the '\' character in your regular expression.

Anyways, in regular expressions if you want to match any character within a "set of characters" you simply have to put that set of characters within square brackets "[]".

So, for the regular expression to match any of the above "special characters" you would have:
Expand|Select|Wrap|Line Numbers
  1. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]"
(I have escaped out all of the wild cards and because the above code is for a VB.NET string I had to use the VB.NET escape code for the " character too).

That only matches punctuation characters though and your requirement includes "white space" as well.

In regular expression syntax the way to indicate that you are looking for white space (in .NET) is to use "\s".

So, we just add "\s" to the set (put it within the square brackets) and it should be the full pattern for what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]"
Now all you have to do is use regular expressions controls that .NET provides you to and ask it for anything in the input string that matches the regular expression:
Expand|Select|Wrap|Line Numbers
  1. Dim input As String = "Hi! Hello, How are you?"
  2. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]"
  3. Dim matches As MatchCollection = Regex.Matches(input, pattern)
  4. MessageBox.Show(String.Format("There are {0} special characters in the input '{1}'", matches.Count, input))

The topic of regular expressions can get pretty complicated but it is truly amazing because regular expressions can accomplish so much in a very elegant way.

I mean, look at what you had to do to accomplish this task without regular expressions...you needed loops, functions to check characters, counters etc etc..

And look at what you have to do to accomplish this using regular expressions: a well thought out pattern and one function call.


Look through the links I posted and use google to research this more. Take your time with this topic because it's complicated.

Ask for help with it (I'm not a huge expert on regular expressions but I can usually get by when asked to do something) and definitely ask your teacher to properly cover this topic because it's too big to cover here.

-Frinny

Share this Question
Share on Google+
11 Replies


Frinavale
Expert Mod 5K+
P: 9,731
In the button click event handler implement code that retrieves text from the from the txtName.

Once you have the string, you should loop through the characters within it and increment a counter every time you find a "special character".

Or you could try using the String.IndexOfAny Method...

Or you could consider using regular expressions to try an achieve the same thing.

-Frinny
Jan 15 '15 #2

Ajay Bhalala
100+
P: 119
Oh! Frinny thank you so much for your reply. I would try it.
Jan 16 '15 #3

Ajay Bhalala
100+
P: 119
Hello Frinny,

I have visited and read your link, but I can't understand how to use the code.

I understand, what you say in
"Once you have the string, you should loop through the characters within it and increment a counter every time you find a "special character".", but I want to know how to check if the given character is special character or not

For example,
If I enter "Hi! Hello, How are you?" into the txtName then MsgBox should display the answer 7 on btnCountChar's click event.

Can you explain me how to count special characters available in the entered string?

Thank you in advance for your reply and also thank you for your help...
Jan 16 '15 #4

Frinavale
Expert Mod 5K+
P: 9,731
What method are you taking on?
What have you tried so far?
Jan 16 '15 #5

Ajay Bhalala
100+
P: 119
Yes, I have tried it. I write as follows

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCountSpecialChar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCountSpecialChar.Click
  2.         Dim a As String
  3.         a = CStr(TextBox1.Text)
  4.         b = CStr(TextBox2.Text)
  5.         Dim l1 As Integer
  6.         l1 = Len(a)
  7.         Dim ch1 As Char
  8.         Dim i As Integer
  9.         Dim count1 As Integer
  10.         For i = 0 To l1 - 1
  11.             ch1 = a.Substring(i, 1)
  12.             If Char.IsWhiteSpace(ch1) = True Or Char.IsSymbol(ch1) = True Or Char.IsDigit(ch1) = True Or Char.IsNumber(ch1) = True Or Char.IsPunctuation(ch1) = True Or Char.IsSeparator(ch1) = True Then
  13.                 count1 = count1 + 1
  14.             End If
  15.         Next
  16.         MsgBox("There are " & count1 & " Special Characters are entered in the TextBox1.", MsgBoxStyle.Information, "Count Special Characters")
  17. End Sub
  18.  
It is done successfully, but is there is any other method is available or not?
If there is another method is available, then please give me the information about the another method.

Thank you for your reply. And Thanks lot for your help.
Jan 16 '15 #6

Frinavale
Expert Mod 5K+
P: 9,731
That looks like it should work.
What's wrong with it?
Jan 16 '15 #7

Ajay Bhalala
100+
P: 119
It's properly work, but my professor said to do this with another method. So I want to count special characters with another method.

Is there is any another method is available or not? If you have any idea, then please give me information about this.
Jan 16 '15 #8

Frinavale
Expert Mod 5K+
P: 9,731
Have you learned about using Regular Expressions?

They are amazingly powerful...you describe what you are looking for based on a pattern and then ask the regular expression if anything in your input matches that pattern.

The regular expression pattern syntax can get tricky sometimes...but your pattern would be really easy.

Your pattern would be: are there any characters in my input that match "This Set Of Special Characters".

If your "special characters" are just punctuation they would include:
Expand|Select|Wrap|Line Numbers
  1. !"#$%&'()*+,-./:;<=>?@[\]^_`{|}
Some of those special characters are used in regular expression pattern syntax or they represent wild cards in regular expressions and so you need to negate them using the '\' character in your regular expression.

Anyways, in regular expressions if you want to match any character within a "set of characters" you simply have to put that set of characters within square brackets "[]".

So, for the regular expression to match any of the above "special characters" you would have:
Expand|Select|Wrap|Line Numbers
  1. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]"
(I have escaped out all of the wild cards and because the above code is for a VB.NET string I had to use the VB.NET escape code for the " character too).

That only matches punctuation characters though and your requirement includes "white space" as well.

In regular expression syntax the way to indicate that you are looking for white space (in .NET) is to use "\s".

So, we just add "\s" to the set (put it within the square brackets) and it should be the full pattern for what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]"
Now all you have to do is use regular expressions controls that .NET provides you to and ask it for anything in the input string that matches the regular expression:
Expand|Select|Wrap|Line Numbers
  1. Dim input As String = "Hi! Hello, How are you?"
  2. Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]"
  3. Dim matches As MatchCollection = Regex.Matches(input, pattern)
  4. MessageBox.Show(String.Format("There are {0} special characters in the input '{1}'", matches.Count, input))

The topic of regular expressions can get pretty complicated but it is truly amazing because regular expressions can accomplish so much in a very elegant way.

I mean, look at what you had to do to accomplish this task without regular expressions...you needed loops, functions to check characters, counters etc etc..

And look at what you have to do to accomplish this using regular expressions: a well thought out pattern and one function call.


Look through the links I posted and use google to research this more. Take your time with this topic because it's complicated.

Ask for help with it (I'm not a huge expert on regular expressions but I can usually get by when asked to do something) and definitely ask your teacher to properly cover this topic because it's too big to cover here.

-Frinny
Jan 16 '15 #9

Ajay Bhalala
100+
P: 119
Wow...! Frinny, its very very helpful for me. I will also ask my teacher. Thank you so much for your help.

I will try it.

I have saved your links. I will read it after my submissions. Because I have to submit my VB.NET journal on 19th January.

Thank you very very very much for your help.

and I have to write the exercise including all the inbuilt functions. So frinny, can you provide me the list of all VB.NET inbuilt functions?

Thank you for your help.
Jan 17 '15 #10

Frinavale
Expert Mod 5K+
P: 9,731
Checkout all of the String Methods available and see what you can use.

Whenever you need to know about a control or built-in functionality that is available through the .NET framework you should refer to the MSDN Library.

Use the search tools in the top right corner and when the search results are displayed be sure to filter them down to "library" (by placing a checkmark in the "Library" filter displayed in the search results) or else you end up with a bunch of stuff that may not be relevant to looking up the Documentation about a particular control/function. (For example: here is the "Library" filtered down search results after searching for String)

Honestly, you'll find a lot more information there about everything in .NET than I know.

-Frinny
Jan 19 '15 #11

Ajay Bhalala
100+
P: 119
Ok Frinny,
Thank you so much for your help. Your all the links are very useful for me. And specially, "String" link is very very very help full for me.

Thank you very much.
Jan 21 '15 #12

Post your reply

Sign in to post your reply or Sign up for a free account.