Really need Help 
Author Message
 Really need Help



Quote:

>Is there a command in Pascal similiar to left or right, i.e. so that you
>can pick say the second character from the left ?

>Any help really appreciated

Since you mention Left and Right I'm guessing you're familiar with
some dialect of BASIC and I assume you mean "pick say the second
character from the left" out of a string?  

The standard Borland Pascal Copy() function can do all the things that
you may be familiar with from BASIC, but if you're looking to extract
a single character from a known location then you should also be aware
that Borland Pascal strings also behave rather like arrays of
characters, so you can index them (starting at 1).

To use your example:

var
  C : char;
  S : string;
begin
  S := 'This is a string';
  { get the second character from the left }
  C := S[2];  { C now contains the character 'h' }

One thing that tends to confuse Pascal newbies is that the type char
and a string containing a single character are not the same thing.  

The Copy() function I mentioned earlier returns a string; so you can
get yourself in trouble if you try to extract single characters with
it, e.g.:

var
  C : char;
  S1, S2 : string;
begin
  S1 := 'This is a string';

  {this won't work, compiler complains about incompatible types:}
  {C := Copy(S1, 2, 1);}

  {you can create a string starting at S1[2] extending for 1 char:}
  S2 := Copy(S1, 2, 1); {S2 now conains the STRING 'h'}

  {get the lone character out of S2:}
  C := S2[1];  

HTH

Stephen Posey



Thu, 10 Apr 2003 09:57:53 GMT  
 Really need Help

Is there a command in Pascal similiar to left or right, i.e. so that you
can pick say the second character from the left ?

Any help really appreciated

G Smith



Wed, 18 Jun 1902 08:00:00 GMT  
 Really need Help


Quote:

> Is there a command in Pascal similiar to left or right, i.e. so that you
> can pick say the second character from the left ?

> Any help really appreciated

> G Smith

Perhaps, as a simple explanation:
copy (s, a, b)
works just like
MID$ (s$, a%, b%)
with the exception that you cannot write to "copy(...)".
Using copy() to get everything from a given position (like MID$ (s$, i%)),
you could use
copy (s, i, 255). If you go out of the string using copy(), the output
string will end and there will be no trouble (other than using a[3]).

Try:

var s: string;
procedure xx;
begin
    writeln (s[1]);
end;

begin
    s := 'Hello';
    xx;
    s := '';
    xx;
end.

--
Rudolf Polzer
REBOUNCE - http://www.mycgiserver.com/~rebounce
I wish I was what I was when I wished I was what I am now.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Come'on guys...I really need help...

2. Filter :I really need help !

3. I really need help with this one

4. I really need help with this one

5. I really need Help!!!

6. Really need help on Database project!

7. Really need help on Database project!

8. Really need help! How to wrap long records in QR 1.1?

9. help a realy really really new person at pascal

10. Database access: really need some help

11. I really need ur help.......thanks

12. I really need ur help...

 

 
Powered by phpBB® Forum Software