Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi 6...

Status
Not open for further replies.

Tshepo2018

Programmer
May 16, 2018
6
ZA
I m still using Delphi 6, is there anyone out there who can help me out here, I need a procedure where I can copy the first character in a string and the first character in the substring of the very string. I using this to create initials if more than one name is entered in a TEdit.text. I have used the following coding for instance:

Code:
Var
Name,Initial : String;
begin  
Name:=Edit.text;
Initial:=Copy(Name,1,1);

now the problem is trying to detect where another name start so I can then carry on copying
the following character, if ever there is another name ofcourse...thanking you in advance

 
try something like this:
Code:
function GetInitials(const Value: String): String;
var
  SL: TStringList;
  I: Integer;
begin
  //Doesn't guard against having more than one consecutive space or nothing but spaces in Value...
  Result := '';
  SL := TStringList.Create;
  try
    Result := '';
    SL.Text := ReplaceText(Value, ' ', #13#10);
    for I := 0 to SL.Count - 1 do
      Result := Result + copy(SL[I][0]);
  finally
    SL.Free;
  end;
end;

use
Code:
var
  Name, Initial: String;
begin
  Name := Edit.Text;
  Initial := GetInitials(Name);
end;
 
Majlumbo, thanks a lot for this great code, however I am
getting errors on the indicated places

SL := TStringList.Create;
try
Result := '';
SL.Text := ReplaceText(Value, ' ', #13#10); {Replace.text}
for I := 0 to SL.Count - 1 do
Result := Result + copy(SL[0]);

{Replace.text} comes out as an undeclared identifier, what conveniently may I
use here....again thanks in advance
 
It's not Replace.Text it is ReplaceText (without the period), but if it's not available in Delphi 6, then try this instead
Code:
SL.Text := stringreplace(Value, ' ', '#13#10', [rfReplaceAll]);
 
Now this is is working for me, I thank you a lot majlumbo, I really was in a tight spot!
 
Dear majlumbo, it seems I have jumped a gun, I thought we have everything in the bag,
however this code :{ Result := Result + copy(SL[0]);}
produces the { element 0 not accessible error} it further says use Setlength or Length ....do you have a way out? I am so sorry after
testing the last code I took it for granted that the problem was solved.
 
Actually, that makes sense, it's a string, and strings in D6 start with 1 not 0, just change 0 to a 1 and it should work.
 
I thought it was an obvious thing also, but it turns out that:
Result := Result + copy(SL[1]) are not enough actual parameters, so says
the compiler, I m wondering how are we going to make this work.
 
The code works perfectly in this context, thanks Majlumbo..the
first character in a string can be easily isolated. The problem though is
if the string has two names, how do I get the first character of each name.
Let me try and show it this way
---------------
var
X: String;
begin
x:=Edit1.Text;
---------------
Question is, if in my Edit1.Text I have Mary Jane, as first names, how do
I isolate M and J alone. initially that was my question, using your code I will be only
be able to isolate the M in Mary, any input? By the way I appreciate your time and
efforts a lot, highest regards.


 
Each string within the string list contains one word. So SL[0][1] is the first character of the first word, SL[1][1] is the first character of the second word. You need the loop like majlumbo provided (note: I've changed the variable name to differentiate the J and the 1 more clearly and also incorporated the edits):
Code:
    for J := 0 to SL.Count - 1 do
      Result := Result + SL[J][1];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top