Solved

Detecting the first char in a string.

Posted on 2003-11-27
10
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…

740 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