merge/seperate

how can i merge to strings together, by putting the letters of string2 as every second letter.

Example:

i have 3 memo/edit boxes, i type

'hlowrd' in box 1, and
'el ol' in box 2,

then merge them together into the 3rd box as 'hello world'.

also how can i reverse it the other way, seperating the string?

Comments

  • : how can i merge to strings together, by putting the letters of string2 as every second letter.
    :
    : Example:
    :
    : i have 3 memo/edit boxes, i type
    :
    : 'hlowrd' in box 1, and
    : 'el ol' in box 2,
    :
    : then merge them together into the 3rd box as 'hello world'.
    :
    : also how can i reverse it the other way, seperating the string?
    :
    Merging strings is quite easy:
    [code]
    if Length(S1) > Length(S2) then // first get the length of the longest string
    c := Length(S1)
    else
    c := Length(S2);
    Result := '';
    for i := 1 to c do begin
    if i < Length(S1) then // Check if counter is valid
    Result := Result + S1[i]; // Add a letter
    if i < Length(S2) then // Check if counter is valid
    Result := Result + S2[i]; // Add a letter
    end;
    [/code]
    Separating is just as easy:
    [code]
    S1 := '';
    S2 := '';
    for i := 1 to Length(Result) do begin
    if i mod 2 = 1 then // if remainder is 1 then
    S1 := S1 + Result[i] // add to string 1 else
    else
    S2 := S2 + Result[i] // add to string 2
    end;
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion