Forum > Beginners
Cancatenate Char to String
vks:
Hello
How do I concatenate a character at the end of a string?
This is the code i have written and it shows Incompatible types!
--- Code: ---var
i, j : Integer;
Item, sList : String;
Label
x;
begin
sList := '';
Item := '"1=ABC,2=XYZ"';
x : while i <= Length(Item)-1 do
if Item[i] = ',' then begin
i := i + 1; j := j + 1;
goto x;
end
else begin
sList[j] := sList[j] + Item[i];
end;
end;
--- End code ---
It basically sepatares based on encountering ',' character ans saves it to StringArray
required output:
sList[0] : 1=ABC
sList[1] : 2=XYZ
derek.john.evans:
Are you saying you want a string split function?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses StrUtils, Types; function StringSplit(const AString: String; const ADelimiter: Char = ','): TStringDynArray;var LPos, LEnd: Integer;begin LPos := 1; while LPos <= Length(AString) + 1 do begin LEnd := PosEx(ADelimiter, AString, LPos); if LEnd = 0 then begin LEnd := Length(AString) + 1; end; SetLength(Result, Length(Result) + 1); Result[High(Result)] := Copy(AString, LPos, LEnd - LPos); LPos := LEnd + 1; end;end;
balazsszekely:
Perhaps the easiest solution is:
--- Code: ---var
SL: TStringList;
I: Integer;
begin
SL := TStringList.Create;
try
SL.DelimitedText := '1=ABC,2=XYZ';
SL.Delimiter := ',';
for I := 0 to SL.Count - 1 do
ShowMessage(SL.Strings[I]);
finally
SL.Free;
end;
end;
--- End code ---
Please note: on large strings this method is slow.
molly:
And to stay as close to OP solution (in which case OP is able to see his/her errors)
--- Code: ---Uses SysUtils;
var
i, j : Integer;
Item : String;
sList : Array[0..10] of string;
begin
// Clear the list array
For i := low(slist) to high(slist) do sList[i] := '';
Item := '"1=ABC,2=XYZ"';
i := 1;
j := 0;
while i <= Length(Item)-1 do
begin
if Item[i] = ',' then
begin
j := j + 1;
end
else
begin
sList[j] := sList[j] + Item[i];
end;
i := i + 1;
end;
// show the list array
For i := low(slist) to high(slist) do
If (length(slist[i]) > 0) then writeln('entry ', inttostr(i), ' = ', slist[i]);
end;
--- End code ---
eny:
@vks: did you notice how none of the above solutions uses a 'label' let alone a 'goto'?
You never need a goto!
Navigation
[0] Message Index
[#] Next page