Solved

Detecting the first char in a string.

Posted on 2003-11-27
10
271 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now