You are here

Different String Conversion function in Delphi

Submitted by Asif Nowaj, Last Modified on 2019-11-22

Now we will discuss about different functions that will convert from string to some other type. If you want to convert string to Boolean use below function.

function StringToBool(s: string): Boolean;
  s := LowerCase(s);
  Result := (s = 'true');

If you want to convert Boolean to string, use below function.

function BoolToString(b: Boolean): string;
  if b = true then
    Result := 'true'
    Result := 'false';

There are different colours object in Delphi called TColor which is used to specify the color of the windows-only control. This is used by the Color property of many components. This is within Graphics unit. Some of the TColor values are clAqua, clBlack, clBlue etc.
In built function Graphics.ColorToString will return TColor value along with ‘cl’ prepended.
If you want to convert a TColor type to string without ‘cl’ prepended, then you can use below function.

function Color2String(Color: TColor): string;
  Result := LowerCase(ColorToString(Color));
  if Copy(Result, 1, 2) = 'cl' then
    Delete(Result, 1, 2);

Similarly, inbuilt function Graphics.StringToColor will return TColor from string. But if you want to get TColor from just color name then use below function. Here StringIsHex is being used, for detail see Different String type verification functions in Delphi

function String2Color(s: string): TColor;
  if StringIsHex(s) = false then
    s := 'cl' + s;
  Result := StringToColor(s);

To convert FontStyles to string, use below function. TFontStyles specify style characteristics of a font. This is defined in Graphics unit.
TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
TFontStyles = set of TFontStyle
Below are the list of possible values of the TFontStyle type.
fsBold-The font is boldfaced.
fsItalic-The font is italicized.
fsUnderline-The font is underlined.
fsStrikeOut-The font is displayed with a horizontal line through it.
Here TrimCharRight is being used, for detail see Different Types of Trim Functions in Delphi

function FontStylesToString(FontStyles: TFontStyles): string; 
  Result := '';
  if fsBold in FontStyles then Result := Result + IntToStr(Ord(fsBold)) + ',';
  if fsItalic in FontStyles then Result := Result + IntToStr(Ord(fsItalic)) + ',';
  if fsUnderline in FontStyles then Result := Result + IntToStr(Ord(fsUnderline)) + ',';
  if fsStrikeOut in FontStyles then Result := Result + IntToStr(Ord(fsStrikeOut)) + ',';
  Result := TrimCharRight(Result, ',');

Similarly, to convert string to FontStyle use below function. Here StringGetNumberOfWords function is used, for details see Get Number of Words within a string in Delphi. Also another function GetWord is used, see details Get Word and Position from String in Delphi

function StringToFontStyles(s: string): TFontStyles;
  i: integer;
  Result := [];
  for i := 1 to StringGetNumberOfWords(s, ',') do
    Result := Result + [TFontStyle(StrToInt(GetWord(s, ',', i)))];

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.