Search code examples
androiddelphifiremonkey

How to convert String to PWideChar in Android Platform using Delphi


how to convert string to PwideChar in Android Platform using Delphi ? in windows apps its done using..

var    
  PW: PWideChar;
begin
  PW := pwidechar(widestring(String));
  PW := pwidechar(widestring(Reply));
  A := ExistWordInString(PW,String,[soWholeWord,soDown]); //A : Boolean

....
end;

the problems is Undeclared identifier: 'WideString' , how to work around this ?

Delphi 10 Berlin , Firemonkey, Android

UPDATE

well, according to http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Migrating_Delphi_Code_to_Mobile_from_Desktop ,, we cant use widestring, i cant think of another way to use string this function :

function ExistWordInString(aString:PWideChar;aSearchString:string;aSearchOptions: TStringSearchOptions): Boolean;
var
  Size : Integer;
Begin
  Size:=StrLen(aString);
  Result := SearchBuf(aString, Size, 0, 0, aSearchString, aSearchOptions)<>nil;

Solution

  • Your code is not strictly correct in Windows. Yes you can convert string (an alias for UnicodeString) to the COM WideString, but this is a waste of time and resources. The correct code is:

    var    
      P: PWideChar;
      S: string;
    ....
    P := PWideChar(S);
    

    In fact, since you are using a Unicode version of Delphi, it is probably idiomatic to use PChar (an alias for PWideChar), to fit alongside string.

    So I would write:

    var    
      P: PChar;
      S: string;
    ....
    P := PChar(S);
    

    Now, this code, as well as being the correct way to do this on Windows, works equally on all platforms.