i just have myvar="8-56-7-1254-88-6"
How to count value between "-"
In my case are 6
COUNT value in string var
-
- PlatinumLounger
- Posts: 4576
- Joined: 26 Apr 2010, 17:36
COUNT value in string var
Last edited by HansV on 11 Dec 2024, 17:03, edited 1 time in total.
-
- PlutoniumLounger
- Posts: 16801
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: COUNT value in strng var
Count the number of "-", and then add 1 to that result.
(later) If you write this as a Function, include the delimiter (in this case the hyphen character) to make the function generally available for any delimited strings.
(later still) Your function could have a multi-character string as the delimiter parameter. This would let you distinguish between " - " and "-" as delimiters.
Cheers, Chris
Fill your face with laughter, then there will be no room for tears.
-
- Administrator
- Posts: 80088
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: COUNT value in strng var
Din conta As Long
conta = Len(myvar) - Len(Replace(myvar, "-", "")) + 1
conta = Len(myvar) - Len(Replace(myvar, "-", "")) + 1
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 742
- Joined: 27 Jun 2021, 10:46
Re: COUNT value in strng var
I suspect sal21's answer will be along the lines of "I don't know how to do that", so here's one possible solution (that meets the requirements resulting from ChrisGreave's suggestions):
Code: Select all
Public Function counter(strSrc As String, strDelimiter As String) As Long
counter = UBound(Split(strSrc, strDelimiter)) + 1
End Function
-
- PlutoniumLounger
- Posts: 16801
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: COUNT value in strng var
Well!
I shall not let that solution go unchallenged.

Here, IMNSHO, is a better solution than SpeakEasy's solution.
MY solution is self-testing



Code: Select all
Public Function counter(strSrc As String, strDelimiter As String) As Long
counter = UBound(Split(strSrc, strDelimiter)) + 1
'Sub TESTcounter()
' Debug.Assert 6 = counter("8-56-7-1254-88-6", "-")
' Debug.Assert 1 = counter("8-56-7-1254-88-6", "+")
' Debug.Assert 0 = counter("", "-")
' Debug.Assert 7 = counter("8-56-7-1254--88-6", "-")
'End Sub
End Function
Fill your face with laughter, then there will be no room for tears.
-
- PlutoniumLounger
- Posts: 16801
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: COUNT value in strng var
I like this; I was trying to remember how to do this in APL, but I didn't even get close to this.



Maybe it's time for me to hang up my keypunch machine.
Cheers, Chris
Fill your face with laughter, then there will be no room for tears.
-
- Administrator
- Posts: 80088
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: COUNT value in string var
APL - it's more than 45 years since I last programmed in APL... 

Best wishes,
Hans
Hans
-
- PlutoniumLounger
- Posts: 16801
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: COUNT value in string var
Paradise!
I had to help code an interpreter that ran as a batch job on 80 column punched cards.
We had lovely operators such as %CEI, %FLO, %RHO etc. There usually wasn't much space left on a card once we got to %LAM and began a comment ...
Cheers, Chris
Fill your face with laughter, then there will be no room for tears.