COUNT value in string var

User avatar
sal21
PlatinumLounger
Posts: 4605
Joined: 26 Apr 2010, 17:36

COUNT value in string var

Post by sal21 »

i just have myvar="8-56-7-1254-88-6"

How to count value between "-"

In my case are 6
Last edited by HansV on 11 Dec 2024, 17:03, edited 1 time in total.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16932
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: COUNT value in strng var

Post by ChrisGreaves »

sal21 wrote:
11 Dec 2024, 14:44
How to count value between "-"
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
Never panic in a room that holds a computer.

User avatar
HansV
Administrator
Posts: 80223
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: COUNT value in strng var

Post by HansV »

Din conta As Long
conta = Len(myvar) - Len(Replace(myvar, "-", "")) + 1
Best wishes,
Hans

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: COUNT value in strng var

Post by SpeakEasy »

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

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16932
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: COUNT value in strng var

Post by ChrisGreaves »

SpeakEasy wrote:
11 Dec 2024, 16:27
... so here's one possible solution (that meets the requirements resulting from ChrisGreave's suggestions):
Well!
I shall not let that solution go unchallenged. :grin:

Here, IMNSHO, is a better solution than SpeakEasy's solution.

MY solution is self-testing :clapping: :thumbup: :fanfare:

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
Cheers, Chris
Never panic in a room that holds a computer.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16932
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: COUNT value in strng var

Post by ChrisGreaves »

HansV wrote:
11 Dec 2024, 16:20
conta = Len(myvar) - Len(Replace(myvar, "-", "")) + 1
I like this; I was trying to remember how to do this in APL, but I didn't even get close to this. :clapping: :clapping: :clapping:

Maybe it's time for me to hang up my keypunch machine.
Cheers, Chris
Never panic in a room that holds a computer.

User avatar
HansV
Administrator
Posts: 80223
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: COUNT value in string var

Post by HansV »

APL - it's more than 45 years since I last programmed in APL... :gramps:
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16932
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: COUNT value in string var

Post by ChrisGreaves »

HansV wrote:
11 Dec 2024, 18:08
APL - it's more than 45 years ago since I last programmed in APL... :gramps:
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
Never panic in a room that holds a computer.