473,387 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to count special characters?

Ajay Bhalala
119 64KB
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

11 5410
Frinavale
9,735 Expert Mod 8TB
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
119 64KB
Oh! Frinny thank you so much for your reply. I would try it.
Jan 16 '15 #3
Ajay Bhalala
119 64KB
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
9,735 Expert Mod 8TB
What method are you taking on?
What have you tried so far?
Jan 16 '15 #5
Ajay Bhalala
119 64KB
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
9,735 Expert Mod 8TB
That looks like it should work.
What's wrong with it?
Jan 16 '15 #7
Ajay Bhalala
119 64KB
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
9,735 Expert Mod 8TB
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
119 64KB
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
9,735 Expert Mod 8TB
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
119 64KB
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

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

Similar topics

2
by: Jerry | last post by:
Hi - Using XSL I need to count the number of times character appears in a string. My guess is I need to recurse through the value of x, but I'm having trouble getting my head around the...
9
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control,...
7
by: bb nicole | last post by:
Is it have any php coding can count the character of messages, and after the message's character dint excess 160 character, it is first message, excess 160, it will show that the message is second...
2
by: Vikks | last post by:
Hello Guys! I want to count a special character(like ~) in my applcation, like how many time it occurs . I tried to use the linecount function and Regex function too... but didn't got...
5
by: Dean | last post by:
Hi, I have a table with non-unique identifiers. I need to take all the values with the same ID's and combine them into one field with a semicolon as a seperator. These values may exceed 255...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
2
by: lengyun | last post by:
Hi there, I've just started to learn c and there's some sections which i have no idea of. I'm hoping some one can help me. I'm required to write a program which counts the number of characters and...
3
by: globomike | last post by:
Hi, I am looking for a function (or hint) how I can count unicode characters (and not bytes) in DB2 V8.2. In DB2 9 the length function is extenced with the codeunits32 option - and this is...
8
by: xiaolim | last post by:
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters. #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.