Rozdzielenie stringa na kilka stringów

0

Witajcie!
Jestem początkującym, i szukam funkcji rozdzielającej stringa na kilka zmiennych (stringów), tzn.
użytkownik pisze coś takiego:
pisz 2,3
I teraz poszukuje funkcji, któraby pisz, 2 i 3 wpisała do osobnych stringów.
Pomóżcie mi coś takiego znaleŹć/napisać :(

0

@Bobi - jesteś WIELKI! Dzięki wielkie! :)
Mam teraz tylko jeden problem:
Próbuje dodac następną funkcje copy :

x:=Edit1.Text;
Label1.Caption:=copy(x,1,pos(' ',x)-1);
Label2.Caption:=copy(x,pos(' ',x)+1,length(x)-pos(',',x)+1);
Label3.Caption:=copy(x,pos(',',x)+1,length(x)-pos(',',x)+1);

I wszystko jest fajnie, tylko ze po wpisaniu test test,2
w Label2 mam test,2, i za boga mi nie wychodzi aby to wyciąć...Co robie źle?

0

Musisz sam do tego dojść metodą prób i błędów , tak własnie nauczysz się programować , poza tym ja nie wiem o co ci do końca chodzi :O
Spróbuj to dokłanie opisać , napisz jeszcze raz , przeczytaj to z 5 razy i daj wyślij , wtedy spróbuje Ci pomóc :)

0

sprubuj <ort>uŻyć </ort>czegoś bardziej unidersalnego


unit StrGet;

interface

uses
  SysUtils, Math;

procedure Scanf(const Text, Format: String; const arg: array of Pointer);
function HexToInt(Hex: String): Integer;

const
  sepa: array[0..1, 0..1]of String =(('\t', #9), ('\n', #10#13));

implementation

type
  Tgg = record
    Typ: Byte;// Typ danej;
    Sep: String;
    vart: String;
  end;

function HexToInt(Hex: String): Integer;
var
  char: string;
  D: Integer;
  Len, i, k, m: Integer;
begin
  Result:= 0;
  d:= 0;
  k:= 0;
  Len:= Length(Hex);
  Hex:= UpperCase(Hex);
  for i:= Len - 1 downto 0 do begin
      Char:= Copy(Hex, i + 1, 1);
      if char = '0' then d:= 0;
      if char = '1' then d:= 1;
      if char = '2' then d:= 2;
      if char = '3' then d:= 3;
      if char = '4' then d:= 4;
      if char = '5' then d:= 5;
      if char = '6' then d:= 6;
      if char = '7' then d:= 7;
      if char = '8' then d:= 8;
      if char = '9' then d:= 9;
      if char = 'A' then d:= 10;
      if char = 'B' then d:= 11;
      if char = 'C' then d:= 12;
      if char = 'D' then d:= 13;
      if char = 'E' then d:= 14;
      if char = 'F' then d:= 15;
      m:= Round(IntPower(2, k));
      Result:= Result + (m * d);
      inc(k, 4);
  end;
end;

procedure GetSeparators(const s: String; var ar: array of Tgg);
var
  i, l, k: Integer;
  sep: String;
begin
  k:= 0;
  i:= 1;
  l:= 1;
  while i < Length(s) do begin
    if s[i] = '%' then begin
       ar[k].Sep:= Copy(s, l, i-l);
       if ar[k].Sep = '\t' then ar[k].Sep:= #9;
       if ar[k].Sep = '\n' then ar[k].Sep:= #10#13;
       inc(k);
       i:= i + 2;
       l:= i;
    end;
    inc(i);
  end;
  sep:= Copy(s, l, i-l);
  ar[k].Sep:= sep;
end;

procedure GetFormats(const Text: String; var ar: array of Tgg);
var
  i, o: Integer;
begin
  i:= 1;
  o:= 0;
  while i < Length(Text) do begin
     if Text[i] = '%' then begin
        inc(i);
        ar[o].Typ:= Ord(Text[i]);
        inc(o);
     end;
     inc(i);
  end;
end;

function GetArguments(const Text: String; var ar: array of Tgg; siz: Integer): Boolean;
var
  i, ind: Integer;
  Tex, tmp: String;
begin
  Result:= False;
  Tex:= Text;
  for i:= 0 to siz do begin
      if ar[i].Sep <> '' then begin
         ind:= Pos(ar[i].Sep, Tex);
         if ind = 0 then Exit// niema takiego ciągu
         else Delete(Tex, 1, ind + Length(ar[i].Sep) - 1);//jest taki ciąg więc go usuwamy
      end;
      tmp:= Tex;
      if ar[i + 1].Sep <> '' then begin
         ind:= Pos(ar[i + 1].Sep, Tex);
         if ind <> 0 then Delete(Tex, ind, Length(Tex));
      end;
      ar[i].vart:= Tex;
      Tex:= tmp;
  end;
  Result:= True;
end;

{type
  TK = record
    case Integer of
    0: (C: PChar);
    1: (S: PString);
    2: (P: Pointer);
  end;    }

procedure Scanf(const Text, Format: String; const arg: array of Pointer);
var
  Ar: array[0..255]of Tgg;
  i: Integer;
  m: PString;
begin
  FillChar(Ar, 255 * SizeOf(Tgg), 0);
  GetFormats(Format, Ar);
  GetSeparators(Format, Ar);
  if not GetArguments(Text, ar, Length(arg)-1) then Exit;
  for i:= 0 to Length(arg) - 1 do begin
      case ar[i].Typ of
        100: Integer(arg[i]^) := StrToInt(ar[i].vart);
        105: Integer(arg[i]^) := StrToInt(ar[i].vart);
        101: Extended(arg[i]^):= StrToFloat(ar[i].vart);
        102: Single(arg[i]^)  := StrToFloat(ar[i].vart);
        115: String(arg[i]^)  := Ar[i].vart;
        117: Cardinal(arg[i]^):= StrToInt(ar[i].vart);
        120: Integer(arg[i]^) := HexToInt(ar[i].vart);
      end;
  end;
end;

end.

sposób urzycia:

var
  s1, s2, s3: String;
begin
  Scanf(edit1.Text, '%s %s,%s', [@s1, @s2, @s3]);
  edit2.Text:= s1;
  edit3.Text:= s2;
  edit4.Text:= s3;
0

Funkcja ExtractStrings z modułu Classes

function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings): Integer;

C++ syntax:

extern PACKAGE int __fastcall ExtractStrings(TSysCharSet Separators, TSysCharSet WhiteSpace, char * Content, TStrings Strings);

Description

Use ExtractStrings to fill a string list with the substrings of the null-terminated string specified by Content.

Separators is a set of characters that are used as delimiters, separating the substrings. Carriage returns, newline characters, and quote characters (single or double) are always treated as separators. Separators are ignored when inside a quoted string until the final end quote. (Note that quoted characters can appear in a quoted string if the quote character is doubled.)

WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string.

Content is the null-terminated string to parse into substrings.

Strings is a string list to which all substrings parsed from Content are added. The string list is not cleared by ExtractStrings, so any strings already in the string list are preserved.

ExtractStrings returns the number of strings added to the Strings parameter.

Note: ExtractStrings does not add empty strings to the list.

0
dj napisał(a)

sprubuj <ort>uŻyć </ort>czegoś bardziej unidersalnego

      if char = '0' then d:= 0;
      if char = '1' then d:= 1;
      if char = '2' then d:= 2;
      if char = '3' then d:= 3;
      if char = '4' then d:= 4;
      if char = '5' then d:= 5;
      if char = '6' then d:= 6;
      if char = '7' then d:= 7;
      if char = '8' then d:= 8;
      if char = '9' then d:= 9;
      if char = 'A' then d:= 10;
      if char = 'B' then d:= 11;
      if char = 'C' then d:= 12;
      if char = 'D' then d:= 13;
      if char = 'E' then d:= 14;
      if char = 'F' then d:= 15;

No to jest bardzo uniwersalne... A o Case słyszałeś :>

0

W ogóle po co pisac funkcję HexToInt skoro można StrToInt('$'+HexString).

1 użytkowników online, w tym zalogowanych: 0, gości: 1