INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Contact US
Thanks. We have received your request and will respond promptly.
Log In
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips Forums!
Talk With Other Members
Be Notified Of Responses To Your Posts
Keyword Search
One-Click Access To Your Favorite Forums
Automated Signatures On Your Posts
Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Posting Guidelines
Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Students Click Here
File and Data Processing
How do I count the number of times a character appears in a string by markdmac
Posted: 30 Mar 08 (Edited 13 Jun 12)
markdmac's Enterprise Ready Login Scripts
By Mark D. MacLachlan, The Spiders Parlor
http://www.thespidersparlor.com
Counting the number of times a character appears in a string can be a simple operation. It only requires a single line of code.
CODE CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, ""))
But how does it work?
OK, let's break this command down to get a good understanding of what this is doing.
We will be storing our answer in a variable called CharacterCount .
CODE CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, ""))
Next we determine the length of our text string using Len(MyString) .
CODE CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, ""))
Once we have the current length of our string we will next use the Replace function to replace all of the instances of our target character we want to count, we will replace it with a null. This is done with (Replace(MyString, TargetCharacter, "") . CODE CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, "") )
So we have just removed all of the target characters which has shortened our string. We then check the length of that new string using the Len function.
CODE CharacterCount = Len(MyString) - Len( Replace(MyString, TargetCharacter, ""))
So at this point we have the length of the string from the start and the length of the string after removing our target characters. If we subtract the latter from the former we can see how many characters were removed which tells us how many times the target character was in the string.
CODE CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, ""))
How About A Sample
The below example counts the number of commas in the string.
CODE --> sample sample="this, that, other, thing, too"
CharacterCount = Len(Sample) - Len(Replace(Sample, ",", ""))
WScript.Echo CharacterCount
Happy scripting.
Mark
Back to VBScript FAQ Index
Back to VBScript Forum
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community. It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
Notification Of Responses To Questions
Favorite Forums One Click Access
Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close