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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a Character in a string?

Status
Not open for further replies.

tjc240e

Technical User
Nov 12, 2004
133
US
I've been looking around and obviously i'm a little dense...
How do i find if a character exists in a string of charaters?

Basically i want to find if a SPACE (' ') exists in a text string (edSearch.text). And then how do i find out what location in the string that space is so i can seperate out the before and after characters... using MIDSTR i am guessing...

Anyone anyone... Beuler... Beuler...

TIA
 
hi

use the pos function

pos will return the position of a char in a string

jb
 
Thanks to everyone the POS, COPY features work beautifully... thanks to svanels for the tips sheet... that helps alot to..

I do have another question on the svanels sheet.

Code:
Val( TextVal, Number , Code) which converts String to a number.
  if this is possible the result of code = 0, other wise code indicates the character where 
the error occured

Will this Val feature help me in determining if the string typed in is actually a number?

I have one field that i want them to either type in a person's name or their member id number. But instead of doing a double string search (one for the name and one for the id)... can i use Val to determine if the string is numeric?
I get
TextVal is the string.
Is Number an integer variable i pass to Val?
Is Code an integer variable i pass to Val as well?

TIA
 

See the StrToIntDef (String to Integer with Default) function in SysUtils.pas

 
Textval := '37645x2';
Val( Textval, Number, Code) ---> Code = 6, Number remains unchanged;

Number is a numeric variable (integer, double etc.)
Code is an integer variable

If code = 0 then ..conversion succesfull

To Zathras,
The StrToInt function came with later versions of Delphi, probably they just used the existing pascal functions to "create" these "new" functions.
In worst case there is always a fallback to the basics.


Steven
 

No "probably" about it. That is exactly how StrToInt and StrToIntDef are implemented (SysUtils.pas).

Apparently Val will never fail. It is up to the programmer to carefully test the return code. StrToInt and StrToIntDef should be a little easier to use for most of us, and quite a bit easier to read in the code.

You might consider updating your FAQ to include references to some newer features. I would suggest at the very least to mention the PosEx function along with the Pos function. You might also mention that there are other useful string handling functions in the StrUtils.pas file as well.

 
Point taken Zathras, some time ago we discussed about PosEx which have been implemented in Delphi 7. My only problem is that I still use Delphi 6 (and I will not upgrade in the near future, unless I get a free full blown version from Borland [thumbsup]).
For the newer features I would rely on you guys to point me, since I cannot test in delphi 7, Net or 2005

Regards

Steven
 
svanels,

an upgrade from D6 pro to D2005Pro costs only 350 Euro. I did it and it is worth every penny (or eurocent choose what you like[spin])

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 

Ok. Didn't realize that it was so new. Here is the implementation from StrUtils.pas -- it's too useful of a function not to have in your toolkit. Just copy it to your standard .pas file that holds your home-grown functions.
Code:
function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;
var
  I,X: Integer;
  Len, LenSubStr: Integer;
begin
  if Offset = 1 then
    Result := Pos(SubStr, S)
  else
  begin
    I := Offset;
    LenSubStr := Length(SubStr);
    Len := Length(S) - LenSubStr + 1;
    while I <= Len do
    begin
      if S[I] = SubStr[1] then
      begin
        X := 1;
        while (X < LenSubStr) and (S[I + X] = SubStr[X + 1]) do
          Inc(X);
        if (X = LenSubStr) then
        begin
          Result := I;
          exit;
        end;
      end;
      Inc(I);
    end;
    Result := 0;
  end;
end;
 
whosrdaddy, it is not that I cannot afford the upgrade, but I do have other priorities, it looks strange but programming is not "core" bussiness for me. It does make my professional life easier. Probably my bosses and peers don't even know I am on Tek-Tips and Eng-Tips (my most favorites cyberspace hangouts). The driving force of my programming is laziness.
My ultimate goal is sitting in the office, feet on the table, reading an interesting (technical) book, and once in a while I talk to the computer and the reports, orders, calculations etc are rolling out the printer, or sending themselves by corporate e-mail [thumbsup] Utopia...

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top