markus_ja
markus_ja

Reputation: 2951

Convert WideString to String

How can I replace the empty widestring characters from a string?

e.g: 'H e l l o' convert to 'Hello'

I read an text blob field from a DB, and want to store it in another table. When I read it, I get the extra spaces, when I store it the extra spaces remains, and cannot be read correctly at the next query.

DXE, Firebird 2.5

UPDATE:
I use the IBQuery -> DataSetProvider -> ClientDataSet without desin-time created fields. It seams, that the IBQuery retrieves the data in that wrong format.

Current Write Code:

blobStream := TStringStream.Create;
...
blobStream.WriteString(blobText); //blobText = 'H e l l o';
ibsql.ParamByName('ABLOBCOL').LoadFromStream(blobStream);
...
ibsql.ExecQuery;
...

In the FB database is 'H e l l o' stored, but it must be 'Hello'. Since it seems to be a bug in IBQuery, I need a way to convert that string.

Upvotes: 3

Views: 2097

Answers (1)

David Heffernan
David Heffernan

Reputation: 612963

First of all, I'm not going to attempt to describe how to remove every other character from a string. Whilst that might appear to solve your problem it merely papers thinly over the gaping cracks. What you have here is a classic text encoding mismatch. The real solution to your problem will involve fixing the mismatch.

I suspect the problem arises in code that you have not shown. As I understand your question now, you have a string variable blobText that contains incorrectly encoded text. But the code in the question takes blobText as input, and so the damage has already been done by the time we reach the code in the question. The key to solving this is the code that puts duff data into blobText.

You need to find the code which assigns to blobText and sort out the encoding problem there. It looks like you have taken UTF-16 encoded text and interpreted it as if it had an 8 bit encoding. I could speculate as to how that would happen, but it would be better for you to look at the actual code, the code that assigns to blobText. If you cannot work it out, please do post an update to the question.

I'm pretty confident that there are no bugs in the database libraries and that this is just an encoding mismatch.

Upvotes: 8

Related Questions