Text vs String

Technically, I understand the difference between String and Text types, one being raw and the other having encoding. But in application why do I choose one over the other? I could see a large text field that may be exported or printed as Text and a variable that just needs to hold character data as String. Is there a reason to Dim an array as Text rather than String?..I usually end up Str(xxx)'ing it later when retrieving values (or am I reading the docs wrong on Dim’ing arrays)?

Thanks!

If you aren’t using any of the new Xojo framework classes you can stick with String for now. The new framework uses Text exclusively so it’s really a matter of where your app is going. iOS is Text only as well.

Until Xojo releases more targets and controls in the Xojo framework you are probably safe with string. Others may disagree but mixing and matching String and Text is kind of a pain. Of course, if you want to be more future-proof you might want to start using Text as it really does solve some longstanding issues with the way many people use string.

Thanks Bob, I had a feeling that might be the answer.

But now, in returning a recordset, rs, and retrieving a column value X to a var dim’ed as Text, how do you tag the return value, e.g.,
textVar = rs.Field(“X”).StringValue
I don’t find a “TextValue” and as is an error is raised. It has to have a “.ToText” on the end of StringValue. Using PostgreSQL.
Thanks.

Also, I’m getting errors on things like this:
fieldList is dim’ed at Text and was set with Text data.

fieldList = “(” + Left(fieldList, Len(fieldList) - 2) + “)”

But I get a type mismatch error: Text expected but got String.

Ah, so I even have to do this:

fieldList = “(” + Left(fieldList, Len(fieldList).ToText - 2) + “)”

Yeah, old framework (postgres included in that) likes String. If you’re defining Text you’ll have to explicitly convert strings to Text.

Unless you’re comfortable using Text just use string for now. You’ll probably be happier.

Gotcha. Thanks.

As an aside, you said Strings are “raw” and Text “having encoding”. This is conceptually incorrect.

Text Encoding only applies when you are dealing with bytes. It determines how those bytes are interpreted back to the characters they are meant to represent. Text represents the characters themselves.

Think of this in a different context. If you have a compressed document, you need to know which compression algorithm to use to get back the original data. Was it compressed with Blowfish, AES, TwoFish, etc.? Without knowing that, the bytes that make up the compressed version are meaningless. They must be converted using the proper algorithm.

(This is an extreme example to illustrate the point only.)

But if you are holding the original, uncompressed document, describing it as “having the compression algorithm” would be meaningless. It is the end result after the algorithm was applied to something else.

In this scenario, String would be equivalent to the compressed document and the Text Encoding the compression algorithm. Text is the end result.

Thanks Kem, I know my description was not really accurate but your illustration is enlightening. I tend to look down the road at what will transpire; if I will need to use text sooner or later than I will learn to use it now. Will make maintanence easier too I would think as I upgrade Xojo over time. I’m not savvy on actual byte by byte storage of text vs string but I would assume it looks something like the encoding for an RFT document… a string of codes followed by the actual text. And string would just be the text. The underlying code handling this then needs to now what it is working with…my assumptions.

FWIW, if you decide that you do want to use Text, converting a String is really easy. Like this:

dim s as String = "Hello World" Dim t as Text = s.ToText

Converting back is even easier:

Dim t as Text = "Hello World" dim s as String = t

Just keep in mind that your string MUST have an encoding.

Thanks Greg, but a little fuzzy on “Just keep in mind that your string MUST have an encoding.”?
Just newbie ignorance here…

A String may or may not have an encoding, depending on where it came from. A common error is to read a string from some external source and forget to define an encoding. Later, you get weird display glitches and wonder why. Text forces you to deal with the encoding from the start.

Note that Text operations are typically slower than the corresponding String operations. They are safer, though (for the above mentioned reason).

Also note that in the new framework, when you want to deal with chunks of bytes without an encoding attached, you use MemoryBlocks. (These now have several of the classic String methods, like Left and Right, for convenience.)

For a better explanation of String vs. Text, see here:

https://forum.xojo.com/22534-string-vs-text-an-explanation

Thanks all. Great info for me.

Hello.
I understand the concept behind both (Text and String) but currently I’m still only using STRING in my projects because I honestly would not know where to apply one or the other, and this is what I think many have doubts, if anyone can explain I would appreciate.

Thank you !

Bobs advice is reasonable

OK, so after reading up on grapheme clusters in the article referenced by Kem above I’m quite definitely sure I do not want to know more about encoding :slight_smile:
In terms of me and my apps, I intend to keep my programming environment and projects up to date so I assume that sooner or later I will be in a Xojo environment requiring Text. I will code now with that in mind and learn as much as I can (or need) to make things work.

just be glad grapheme clusters obscure some of the really nasty underpinnings :stuck_out_tongue: