Solved

Detecting the first char in a string.

Posted on 2003-11-27
10
268 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:
mnasman 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

17 Experts available now in Live!

Get 1:1 Help Now