473,624 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Character count in a string

Hi!

I have a question regarding the count if character within a string
like for example I have a string of

e.g.

123#123#

I would like to determine what is the code? of getting the # sign
which the answer is 2.
Hope you can help me on this.
Thanks and Regards..
Nov 21 '05 #1
3 26221
If you're looking to count a specific character in a string, here it is:

public function charcount(byval s as string, byval c as string) as long
dim i as long
dim count as long

count = 0
i = instr(s, c)
do while i > 0
count = count, + 1
i = instr(i + 1, s, c)
loop
charcount = count
end function

The key to this is the second instr call. It starts at the character after
the found location and returns the offset of the next instance. If there
are no more instances, it returns 0.

Mike Ober.
"Kuups" <ma****@comtech solutions.com> wrote in message
news:eL******** *****@TK2MSFTNG P12.phx.gbl...
Hi!

I have a question regarding the count if character within a string
like for example I have a string of

e.g.

123#123#

I would like to determine what is the code? of getting the # sign
which the answer is 2.
Hope you can help me on this.
Thanks and Regards..


Nov 21 '05 #2
I was thinking that maybe the split function would be a way to do it using
the character as the delimiter. But if the character was the first or last
character of the string the count would be off. But it was a thought...

dim tmp() as string
tmp=split(s,c)
dim count as int32 = tmp.length-1

"Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
news:bf******** *********@newsr ead1.news.pas.e arthlink.net...
If you're looking to count a specific character in a string, here it is:

public function charcount(byval s as string, byval c as string) as long
dim i as long
dim count as long

count = 0
i = instr(s, c)
do while i > 0
count = count, + 1
i = instr(i + 1, s, c)
loop
charcount = count
end function

The key to this is the second instr call. It starts at the character
after
the found location and returns the offset of the next instance. If there
are no more instances, it returns 0.

Mike Ober.
"Kuups" <ma****@comtech solutions.com> wrote in message
news:eL******** *****@TK2MSFTNG P12.phx.gbl...
Hi!

I have a question regarding the count if character within a string
like for example I have a string of

e.g.

123#123#

I would like to determine what is the code? of getting the # sign
which the answer is 2.
Hope you can help me on this.
Thanks and Regards..


Nov 21 '05 #3
Terry,

We had once a thread where everybody had given another method for this.
I said joking the split function to add even another one.

Than there became a discussion which one was the quickest.
I did a test in this newsgroup and some gave extra methods

To find a "char" the indexof("#"c) was in that the fastest. Finding a string
the "Instr" in a way as showed by Michael 2 times faster than any other
tested (I did not check that procedure so I assume that it is right).

For a char as asked here are these two faremost the fastest both made by Jay
where the do until loop was slightly faster than the for each.

\\\.
Public Function test5(ByVal input As String, ByVal _
delimiter As Char) As Integer 'Jay 1(char)
Dim count, index As Integer
index = input.IndexOf(d elimiter)
Do Until index < 0
count += 1
index = input.IndexOf(d elimiter, index + 1)
Loop
Return count
End Function
Public Shared Function test6(ByVal input As String, _
ByVal delimiter As Char) As Integer 'JayB 2(char)
Dim count As Integer
For Each ch As Char In input
If ch = delimiter Then
count += 1
End If
Next ch
Return count
End Function
///

(The split was together with regex use the slowest where you would think in
more than 100 times)

I hope this gives some ideas to Kuups as well when he read this.

Cor
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2174
by: Julia Briggs | last post by:
Hello, trying to create logic tests against a string allowing someone to enter a certain character once or twice into a form consecutively, but not twice seperated between other characters.... so, hello##there (ok) hello#there (ok) #hello#there (no) ##hello#there (no)
9
2534
by: refer_to_website | last post by:
My VB.NET web application has a textbox where the user is allowed to enter up to 50 characters. I have a label on the form next to the textbox that tells the user how many remaining characters he can enter. This label needs to be refreshed on the fly each time the user types a character into the text box. Eventually, the user will click the SAVE button on the web form, and the data he entered in the text box will be written to the...
2
2238
by: Dan McCloud | last post by:
Is there anything 'wrong' with setting the value of a drop down menu using the following? document.frmStep1.drpInvaddress.value = 'A1020761603!>\>R2' This string is a key in our database that I have no control over and I want to set the value of the menu according to this value because it is unique. I suspect that the '\' or the '>' or the '!' is making Javascript think that this is a different type of data than string.
9
10096
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, possibly a custom validator with a function written in javascript. Has anyone done this? Does someone have an example? Regards
7
2383
by: P. Schmidt-Volkmar | last post by:
Hi there, I have a string in which I want to calculate how often the character ';' occurs. If the character does not occur 42 times, the ";" should be added so the 42 are reached. My solution is slow and wrong: for Position in range (0, len(Zeile)): if Zeile==';': AnzahlSemikolon = AnzahlSemikolon +1 if AnzahlSemikolon < 42:
8
4725
by: Joe Reynolds | last post by:
say i have a string that is "hello this is my string and i like it very much" and i want to insert a newline at every 20th character, how could i go about doing this?
2
10780
by: si_owen | last post by:
Hi Folks, I am trying to set up some code that will provide a character count for the text entered into a textbox on my form. I am coding in VB.Net using visual studio 2003 - web appliaction. I have sorted the VB code, which when execute via a command button it updates the label. Private Sub txtcomment_TextChanged(ByVal sender As System.Object,
3
1789
by: shaif | last post by:
Hey I have a simple problem that How i can get set of character from String, such as: Dim s as string Dim s1 as string s="73421482716@bffsdffhjxyz783djk"; s1="xyz"; How it is possible that value of s1 if in s then msgbox" Found" Just please give some idea or example. I dont have much knowledge abt string saperation in VB, Is there any built-in function can use to do that search?
7
3055
kireytir
by: kireytir | last post by:
hi all, this is my code (in interrupt function) for finding character in string (data). Ekle_text.Text += data; string veri = Ekle_text.Text; int found = veri.IndexOf("x"); if (found != -1) {
0
8173
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.