Link to home
Create AccountLog in
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Avatar of redsg
redsg

Replacing characters in PWideChar variable
I'm looking for a way to find and replace certain characters within a PWideChar variable in Delphi. The PWideChar variable contains Unicode (specifically, Chinese) characters, with which I'm looking to rename my files with. However, for some of these PWideChar variables, some special characters have been included, such as the colon (':').

How do I then find these special characters, and replace them with say an empty string? I'm aware of how to do this with normal String variables, but how can it be achieved with PWideChar variables?

Your assistance will be much appreciated please. Thanks!

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Pramod BugudaiPramod Bugudai🇮🇳

Hi. Each and every character has a unique UTF8 and UTF16  specific numbers. Like the colon character also has specific no. Also they have their decimal code points.
use this numbers to handle the characters. From a paragraph you can search for a character with it's unique decimal code points. So that you can easily handle them.

I will also suggest you to use WideChar(parameter as a num); for better results.


Here you can convert the characters to their unicode specific nos.
http://people.w3.org/rishida/scripts/uniview/conversion.php

remember  WideChar()  holds a single character that supports International character sets as it's parameter.

Avatar of redsgredsg

ASKER

pramodbugudai,

Thanks for your suggestions. The issue I'm facing now is manipulating the PWideChar variable itself. I'm quite new with Delphi, and even newer with the PWideChar type. I'm therefore looking for a reference with which can be used to manipulate PWideChar variables, much like how String variables can be manipulated i.e. the methods, properties etc.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of HypoHypo🇸🇪

You can always use a temporary string variable, in to which you load the values from the PWideChar, and then use the functions that you are familiar with to do what you want, and when you are done write the result back to the PWideChar...

Delphi handles typecasting from a Widestring to a PWideChar as long as you tell it to...

regards
Hypo
uses ..., StrUtils; 
procedure TForm1.Button1Click(Sender: TObject);
var aWideChar : PWideChar;
    aTempString : WideString;
begin
  // Load the PWideChar-string with some data... 
  aWideChar := '123:321';
  // Move the PWideChar-string into a temp-string...   
  aTempString := aWideChar;
  // Do your stuff... 
  aTempString := ReplaceStr(aWideChar, ':', ' ');
  // Reload the PWideChar with the contents of the string... 
  aWideChar := PWideChar(aTempString);
  // Show results... 
  ShowMessage(aWideChar);
end;

Open in new window


Avatar of redsgredsg

ASKER

Hypo,

I tried the following:

var
   tempName:  WideString;
   newName:  PWideChar
...
newName := PWideChar(TntMemo1.Lines.Strings[index]);
tempName := newName;
tempName := StringReplace(newName, ':', ' ');
newName := PWideChar(tempName);

But when I tried to show the results within another TNTMemo object, the Chinese characters have all been converted to "?"s. I think this occured when the PWideChar variable is changed to the WideString type (i.e. tempName := newName).

Avatar of redsgredsg

ASKER

An update - casting from PWideChar to WideString did not change the characters to "?"s, instead, its after going through the "StringReplace" method that the characters turn out as "?"s.

I noticed that u used the "ReplaceStr" method, what do I need to add to the "Uses.." statement to use this method?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of redsgredsg

ASKER

Found a sort-of workaround i.e. by "splitting" the WideString variable. Before that, I looked for the position of the character(s) concerned, and if found the method attached below is triggered.

Everything seems to work fine for now.
function SplitString(pos: Integer; str: WideString): WideString;
var
   left:   WideString;
   right: WideString;
begin
   left := LeftStr(str, pos-1);
   right := RightStr(str, Length(str)-pos);
   Result := left + right;
end;

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of HypoHypo🇸🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of redsgredsg

ASKER

Thanks for your help Hypo!
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.