How to work with substrings / delete character when content is mixed single and double byte?

139 Views Asked by At

I have an application, which unfortunately is written in Delphi 6, that allows the user to extract substrings / delete characters from input. The operator sees a TLabel, this is on purpose to avoid direct and incorrect input.

This works fine for English text. But sometimes, the text is mixed content (ANSI-encoded), I set the charset and the display works fine without modification.

One trivial example could be:

月123中

The string in hex is 8C 8E | 31 | 32 | 33 | 92 86 (split on characters for clarity)

But once modification starts, things go wrong. I know why - the standard StrLeft / StrRight / SubString works on single bytes - and once I remove a byte, the text renders meaningless.

Do I have to go through a WideString conversion and back every time I modify something? Or are there native libraries / functions in older Delphi versions to do what I need?

Something like this, in pseudocode:

mixedStringLength(content)             = 5        // 5 characters visible
mixedStingLeft(content,2)              = "月1"    // actually the bytes 8C  8E  31
mixedStringDeleteCharsRight(content,1) = "月123"  // actually deletes 2 bytes

As the string in question is used in many other places, and the project is large, it's not as trivial as just using TNT libraries, or switching over to a newer Delphi version.

0

There are 0 best solutions below