Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detecting the first char in a string.

Posted on 2003-11-27
10
Medium Priority
?
278 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 2000 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

Technology Partners: 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!

Question has a verified solution.

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

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…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

824 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