Android Question Detect a single quotation mark?

DrownedBat

Member
Licensed User
Longtime User
How might one detect a single quotation mark in the code? For example, if I have a string variable called TEXT, and I am trying to detect a single quotation mark within it, TEXT.Contains(""") clearly doesn't work. Is there a work around this?
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
How might one detect a single quotation mark in the code? For example, if I have a string variable called TEXT, and I am trying to detect a single quotation mark within it, TEXT.Contains(""") clearly doesn't work. Is there a work around this?
Look into Chr(

RBS
 
Upvote 0

DrownedBat

Member
Licensed User
Longtime User
Chr( says it uses the Unicode Variable, so U+0022 would be the single quotation, but every example seems to use a single integer or "0x000" etc. What is this code called and how do I find the translation?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you are opening a can of worms; a single quotation mark can be
represented by a number of different utf-8 characters.

and your use of the term is not clear: are you talking about
a "single" quotation mark? or a "single quotation mark"? there
is a difference.

chr(34) can refer to a single quotation mark, but the term
"single quotation mark" usually connotes something that can
be thought of as ' (chr(39)). it is used in english speaking countries
in situations such as quotes within a quote: eg,
"and she suddenly exclaimed, 'applesauce!'."

other examples would be the so-called left single quotation mark (chr(0x2018))
and the right single quotation mark, not to mention chr(0x275B), the charming
heavy single turned comma quotation mark. and let's not forget the
left-pointing double angle quotation mark (chr(0xAB)). all of them display nicely
in b4a.

there are many uft-8 tables online.
 
Upvote 0

emexes

Expert
Licensed User
For example, if I have a string variable called TEXT, and I am trying to detect a single quotation mark within it, TEXT.Contains(""") clearly doesn't work. Is there a work around this?

Going by your first draft:

B4X:
TEXT.Contains(""")

which contains a single quotation mark inside the usual string literal delimiting quotation marks, you'll probably find that adding just one more (to be sure, to be sure) would have got you over the finish line eg try:

B4X:
TEXT.Contains("""")

which should return True if TEXT contains a quotation mark character (ASCII/UNICODE 0x22 ie Chr(0x22))

This works because B4X (and most BASIC dialects) use the common trick of "escaping" delimiter characters within a string literal by doubling them, in order to distinguish them from the delimiter character than marks the end of the string literal.

Better would be:

B4X:
TEXT.Contains(QUOTE)

if the above rumours of QUOTE = Chr(0x22) are true.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
common trick of "escaping" delimiter characters within a string literal by doubling them

https://www.b4x.com/android/forum/threads/basic4android-v2-50-beta-is-released.24756/#post-143293

New features
  • Quotes in string literals - Double quotes in string literals will be treated as a single quote:
    B4X:
    Log("Hello ""world""") 'Will log: Hello "World"

1671347343781.png
 
Upvote 0
Top