Counting number of special characters in a string

Hi all,

Seemds simple enough, but I looked at the posting on the site, yet I could not find a way to do this using Lotus script.

I am trying to count the munber of “~” in a string using script…

test = “ABC~DFG~HIJ”

if Number of “~” test > 3 then

do this

else do that

end if

thanks

Subject: Counting number of special characters in a string

Sub Click(Source As Button)

test1$ = "ABC~DFG~HIJ"

cnt1% = Len(test1$)

test2$ = Replace(test1$, "~", "")

cnt2% = Len(test2$)

tildecount% = cnt1% - cnt2%

Msgbox "there are " & Cstr(tildecount%) & " tildes"

End Sub

Subject: RE: Counting number of special characters in a string

Thanks all for the help it is working… now I am working on the second part of my code…

Regardless of the number of “~” in the code I will always need the first and second part of the string.

So if the string is like “ABC~DFG~RDT~RTY” I would always want to get “ABC~DFG”

paul,

Subject: RE: Counting number of special characters in a string

http://www-10.lotus.com/ldd/nd6forum.nsf/ShowMyTopicsAllFlatweb/851c5aea4b40895f852573870063810b?OpenDocument

Subject: RE: Counting number of special characters in a string

yes you are right… but I still need to create this using lotus script…

Subject: RE: Counting number of special characters in a string

Sub Click(Source As Button) Dim s As String

Dim v As Variant

Dim c As Integer

Dim x As String

Dim t As String



s = "ABC~DEF~GHI~HJK"

Gosub DoSplit



s = "ABC~"

Gosub DoSplit



s = "ABC"

Gosub DoSplit



s = "ABC~~DEF~GHI~HJK"

Gosub DoSplit



Exit Sub

DoSplit:

x = ""

t = "NFG"

v = Split(s, "~")

c = Ubound(v)

If c > 0 Then

	x = v(0) & "~" & v(1)

	t = "OK"

End If

Msgbox x, , t

Return

End Sub

Subject: RE: Counting number of special characters in a string

thanks for the help all of you.

Subject: Counting number of special characters in a string

You could define a variant variable, use the split function then use ubound-1 to find how many ~ there are.

something like:

Dim ret As Variant

dim teststr as string

dim chrctr as integer

test = “ABC~DFG~HIJ”

ret = split(test, “~”)

chrctr = ubound(ret)-1

if chrctr > 3 then do this else do that end if

Subject: RE: Counting number of special characters in a string

check chrctr = ubound(ret)-1. Might not need to delete 1 from the result because the index will start at 0

Subject: Counting number of special characters in a string

From Designer Help:

@Explode( string ; separators ; includeEmpties )

x := @Explode(“ABC~DFG~HIJ”; “~”; @True);

NumberOfSeparatorOccurences := @Elements(x)-1;