简体   繁体   English

从查询中提取逗号分隔的字符串值并将它们分配给 Delphi 4 上的不同变量

[英]Extracting comma separated string values from a Query and assign them to different variables on Delphi 4

I'm trying to split a comma separated string from a Query and assign each one in a different variable:我正在尝试从 Query 中拆分逗号分隔的字符串,并将每个字符串分配给不同的变量:

qValidation.Close;
qValidation.SQL.Clear;
qValidation.SQL.Add('SELECT VALUE FROM PARAMETER WHERE CODPARAMETER = ''XYM'' ');
qValidation.Open;

The String I get from this Code (VALUE) is ' 12.5,45.3,33.5,67.9 '我从这段代码(VALUE)得到的字符串是'12.5,45.3,33.5,67.9 '

I want to get each value and assign them in a different variable, ie:我想获取每个值并将它们分配给不同的变量,即:

X1 = 12.5
X2 = 45.3
X3 = 33.5
X4 = 67.9

These variables must be float type这些变量必须是浮点类型

I've read that you can use a TStringList but I don't find a Delphi4 clear way for me我读过您可以使用 TStringList 但我没有找到适合我的 Delphi4 清晰方法

TStringList is declared in the Classes unit. TStringListClasses单元中声明。 It has a CommaText property, which will parse the input text into its Strings[] property.它有一个CommaText属性,它将输入文本解析为它的Strings[]属性。 For each individual string, you can convert it to Extended using the StrToFloat() function in the SysUtils unit, and then assign that value to a Single / Double variable as needed.对于每个单独的字符串,您可以使用SysUtils单元中的StrToFloat() function 将其转换为Extended ,然后根据需要将该值分配给Single / Double变量。

The Delphi 4 library has the necessary functionality. Delphi 4 库具有必要的功能。 This is TStrings.CommaText of unit Classes .这是单元ClassesTStrings.CommaText

TStrings is an abstract class, so must use TStringList TStrings是一个抽象的class,所以必须使用TStringList

program ProjectTest;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  vList: TStrings;
  i: Integer;
begin
  vList := TStringList.Create;
  try
    vList.CommaText := '12.5,45.3,33.5,67.9';

    for i := 0 to vList.Count - 1 do
      Writeln(vList[i]);
  finally
    vList.Free;
  end;

  Readln;
end.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM