Counting letters

I have a text and it has the following in it as an example: “The fox was in the meadow.”

What I seem to be having a difficult time with is counting the number of letters and spaces in the above statement. In Visual Basic I was able to count the letters and also determine what the fifth character was, or the seventh, eleventh or wathever was. While there is a count words example in the forum, I’m having a difficult time putting together how to count words.

Can anyone direct me to where there might be information on doing the above. Thank you.

Dim thisString as String = “The fox was in the meadow.”
Dim thisStringCount as Integer = thisString.Len()

[quote=208900:@James Backus]I have a text and it has the following in it as an example: “The fox was in the meadow.”

What I seem to be having a difficult time with is counting the number of letters and spaces in the above statement. In Visual Basic I was able to count the letters and also determine what the fifth character was, or the seventh, eleventh or wathever was. While there is a count words example in the forum, I’m having a difficult time putting together how to count words.

Can anyone direct me to where there might be information on doing the above. Thank you.[/quote]

Depends on how you define “word” BUT a simple approach would be to simply split on spaces

dim words() as string = text.Split(" ")
dim count as integer = words.ubound() + 1

However this simple approach will have issues.
Runs like “forever,forever” will be counted as one “word”.
So then you think “oh I’ll just replace all punctuation with spaces” so “forever,forever” gets treated like “forever forever”.
That would give you two words BUT a hyphenated word would also count as two.
And words like “you’ll” would get counted as two “words”
So you have to chose carefully what punctuation you replace with spaces (see replaceall)

A regular expression (or regex) might give you slightly better results - but you’ll have to be aware of what it’s criteria are and what issues it might experience

Oops thanks Norman :slight_smile: I read it too fast for length :slight_smile:

Seems to me you would do it pretty much the same as in other languages. Given:
dim s as string = “The fox was in the meadow.”

n = len(s)

c = mid(s,5,1)

Thank you very much for help.

James here is an example using regex as Norman had mentioned above.

This test project first breaks up the text by using Mid and then I run each character through a regex pattern to determine the matching of your criteria.

HTH.

https://www.dropbox.com/s/o5o7vnt2bowom3p/regExCountEx.xojo_binary_project?dl=0