Solved

Detecting the first char in a string.

Posted on 2003-11-27
10
272 Views
Last Modified: 2010-04-05
I'm wanting to detect the first character in a string and do different things depending on what it is.

For instance, if the first character is a "/" then I want to do one thing, if it's anything else then do another thing.  I actually have my program working, the problem is it doesn't do it if it's ONLY the first string.  If a "/" is in the string anywhwere, it still does the string procedure.  Here's my code (found on EE btw):

i := Pos('/',MyString);
    if i <> 0 then begin
      LogMemo1.Lines.Add('This command started with a slash!);
    end else begin
      LogMemo1.Lines.Add('This command does not begin with a slash.);
    end;


Like I said, if it has a slash anywhere in it, it will process the first statement.  Please help.
0
Comment
Question by:Cerixus
10 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 9832881
errrr...

i := Pos('/',MyString);
    if i = 1 then begin
      LogMemo1.Lines.Add('This command started with a slash!);
    end else begin
      LogMemo1.Lines.Add('This command does not begin with a slash.);
    end;
0
 
LVL 1

Author Comment

by:Cerixus
ID: 9832912
If I do what you said, then it reports "This Command Does not begin with a slash." every time I put a slash in ANYWHERE.  For some reason it's backwards.  I did try changing it to <>1 and it works just like it did when I had <>0.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9832973
Don't think so. Are you sure that 'MyString' is correct? What is it? Show some more code. The above works fine for me.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 500 total points
ID: 9832997
Hello

  use LeftStr function instead of Pos to return the first letter in the string and then compare it

  if LeftStr(MyString,1) = '/' then
    LogMemo1.Lines.Add('This command started with a slash!')
  else
    LogMemo1.Lines.Add('This command does not begin with a slash.');


the LeftStr with more string functions can be found in "StrUtils" unit, so you need to add it to the uses clause in your unit

Regards,
Mohammed
0
 
LVL 1

Author Comment

by:Cerixus
ID: 9833142
Works great, thank you!
0
 

Expert Comment

by:wsock32_
ID: 9835417
its a shame to use Pos() or LeftStr() for such a simple thing..
if MyStr[1] = '/' then
LogMemo1.Lines.Add('This command started with a slash!')
else
LogMemo1.Lines.Add('This command does not begin with a slash.');

can it get any simpler? :)
0
 
LVL 1

Author Comment

by:Cerixus
ID: 9835427
I guess it makes sense that all strings are arrays of characters, but I didn't realize it was from a syntax standpoint.  That's pretty simple  :).
0
 
LVL 2

Expert Comment

by:j42
ID: 9836247
Just for completeness:

MyStr[1] may crash if MyStr = '';

I agree with andrewjb. His code is suposed to do exactly what you want. Maybe there's some confusion about leading whitespaces?



Regards
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 9837051
var s : string;
...
if s <> '' then case s[1] of
  'a' : showmessage('the first symbol is "a"');
  'b' : showmessage('the first symbol is "b"');
  'c' : showmessage('the first symbol is "c"');
  'd' : showmessage('the first symbol is "d"');
else
  ShowMessage('the first symbol is unknown');
end;


;)
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9837202
Oi! Mine definitely works. Grrrr....
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question