String constant with quote mark

Tim, in this case, I think using Text removes complexity. String forces you to understand and deal with encodings, where Text does not.

Marc, something else you should know. InStrB reports the byte position of the match, not the character position. Since, in UTF-8, characters may be represented by 1, 2, 3, or 4 bytes, the byte position must be translated to the character position when using InStrB. You can do this easily enough with code like this:

dim p as integer = someString.InStrB( char )
if p <> 0 then
  p = someString.LeftB( p - 1 ).Len + 1
end if

Again, this only applies if you continue to use String and the classic framework calls.

Another way to solve your problem may be to use regular expressions. For example, the search pattern "['‘’]" will find any type of quote mark in your text. If you want to explore that option, let me know.

Dropbox, or a service like it, is a great, free option for doing just this.

Unfortunately I have never been able to wrap my head around “regular expressions” so I don’t know how to build one that would work for me.

With my constant defined as TEXT, and the code for my Search button as shown below …

[code] dim A,B,C as text
dim X,Y,Z as Integer

A = TextField1.text.ToText
B = TextField2.text.ToText
C = MyString.ToText
Label6.text = MyString.ToText
Label10.text = C
X = InstrB(A,B)
Y = InstrB(MyString,B)
Z = InstrB(C,B)
Label1.text = str(X)
Label4.text = str(Y)
Label12.text = str(Z)[/code]

When I search for a capital E I get …
link text

When I search for the Double Quote mark I get …
link text

And when I search for a character that falls after the quote mark, I get …
link text

If I search for the single-quote (that is used in contractions like Don’t) I get …
link text

If I search for a lowercase letter I get …
link text

Changing my code to implement the “IndexOf” method … (assuming I have implemented it correctly)

[code] dim A,B,C as text
dim W,X,Y,Z as Integer

A = TextField1.text.ToText
B = TextField2.text.ToText
C = MyString
Label6.text = MyString
Label10.text = C
X = InstrB(A,B)
Y = InstrB(MyString,B)
W = MyString.IndexOf( B, Text.CompareCaseSensitive )
Z = InstrB(C,B)

Label1.text = str(X)
Label4.text = str(Y)
Label12.text = str(Z)
Label14.text = str(W)[/code]

It successfully distinguishes between uppercase and lowercase letters…
link text

but … it still will not correctly match a quote mark
link text

And this is even after I have changed the constant to TEXT …
link text

Why … oh why … should finding a quote mark in the middle of a string - or text - be such a programming challenge???

Because you’re looking for a needle in a haystack that contains no needles. Your constant simply does not contain a regular quote mark. Turn off smart quotes, re-enter the constant, and get on with your life.

But doesn’t that mean that any user of my program would ALSO have to turn off smart quotes ?

For a constant? That is your code and not the search text and result of your users.

Your code may fail for foreign characters. Please try with something like ü, é or even russian or chinese characters.

For your and our sanity I also recommend that you start using longer variable names. Makes code so much easier to read

[quote]dim SearchText as string = “something”
dim TextToSearch as string = Textfield1.text
dim SearchResult as integer = instr(TextToSearch, SearchText)[/quote]

and so on. Just a recommendation…

Are you using only single-byte characters? IE: ASCII?

Well, I’ll be (expletive) … I entered the constant without smart quotes … but then I turned Smart Quotes back on for testing purposes, and it worked perfectly!

I never ever would have guessed that something like Smart Quotes could cause such a PAIN !

The answer was so simple (although I never would have expected something like that to cause a problem) that now I feel lie a total idiot !

I apologize to everyone for it taking me so long to “get it”

Yes, I was searching for single characters (extracted from a source text as a mid() ) within the constant