Link to home
Start Free TrialLog in
Avatar of Raymond
RaymondFlag for Hong Kong

asked on

Insert TAB into the string.

A string = '12xcvfgr12dfgfrtfgg12werd12......'

I need to insert a TAB before '12' in the string except the first '12'.

Resulting string = '12xcvfgr(TAB)12dfgfrtfgg(TAB)12werd(TAB)12......'

I need the source code, thank you!

raymng
Avatar of GloomyFriar
GloomyFriar

string = '12xcvfgr'+#9+'12dfgfrtfgg'+#9+'12werd'+#9+'12......'
Avatar of kretzschmar
too late :-(
function ProcessString(src: string): string;
var i: integer;
begin
  Delete(src, 1, 2);
  Result := '12';
  repeat
    i := Pos('12', src);
    Result := Result + Copy(src, 1, i - 1) + #9 + '12';
    Delete(src, 1, i + 1);
  until i = 0;
  Result := Result + src;
end;
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is a bug in my previous code ;-)
Here is the tested version:

function ProcessString(src: string): string;
var i: integer;
begin
  Delete(src, 1, 2);
  Result := '12';
  while True do begin
    i := Pos('12', src);
    if i = 0 then break;
    Result := Result + Copy(src, 1, i - 1) + #9 + '12';
    Delete(src, 1, i + 1);
  end;
  Result := Result + src;
end;
You don't need the '+' between a constant string within ' ' and a #9.

'First part'#9'Second Part'  would also be all right. (You might prefer to leave the + for readability though...)
>'First part'#9'Second Part'
It's not readable from my point of view.
The definition for "readability" is such an elusive one... <sigh>

IMHO it might be preferable to skip the '+' when you have a lot of #xxx chars and too many '+' would make the string unconfortably long. (Besides, if you put a lot of #xxx in a string, it's already a hieroglyphic, so who cares any more about readability anyway...)   =)
Also, things like 'First line' + #13#10 + 'Second line'  should be considered -again, IMHO- preferable to 'First line' + #13 + #10 + 'Second line'.

But I do agree it is normally preferable to leave the '+'.
>when you have a lot of #xxx chars and too many '+' would make the string unconfortably long.
agree.
#58#45#41

(Isn't it the longest smiley ever?)