Jump to content
twenty_sleven

Separating a String into values in an Array

Recommended Posts

program Sandbox;
var
i,k,j : Integer;
aStrings: array of string;
sUserInput: String;

begin

aStrings := ['','','','','','','','','','','','',''];

sUserInput := 'Close all applications ';
sUserInput := Uppercase(sUserInput);

k := 0;
j := 0;
//j acts as previous space, k is the current value of the Array we're on

for i:= 1 to Length(sUserInput) - 1 do
 if StrGet(sUserInput,i) = ' ' then
      aStrings[k] := copy(sUserInput, j, i+1);

      j := i;
      k := k+1;
      WriteLn(aStrings[k-1]);
 end.
end.

 

Program to break apart a string, and then put it's separate parts into separate values in an array. This program outputs "CLOSE ALL APPLICATIO"

while I was hoping for...

"CLOSE"

"ALL"

"APPLICATIONS"

 

It seems like the for loop executes all 22 "loops" on the first line of code, I'd like to make that also apply to j := i; & k := k+1;

 

:::EDIT::: Sorry, fixed the problem with the "Explode" function.

Edited by twenty_sleven
Link to comment
Share on other sites

[scar]program Sandbox;

var

I, L: Integer;

aStrings: TStringArray;

sUserInput: string;

begin

sUserInput := 'Close all applications ';

sUserInput := Uppercase(sUserInput);

 

aStrings := Explode(' ', sUserInput);

 

L := Length(aStrings);

for I := 0 to L - 1 do

WriteLn(aStrings);

end.[/scar]

 

Sometimes the solution is just [wiki=Explode]that easy[/wiki]. ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...