Board index » delphi » Converting string[1] to CHAR

Converting string[1] to CHAR

HN>How do you convert a string[1] to a char?

HN>hne...@ac.net

{--------------------------------------}
program Sample;
var
   S: string;
   C: char;

begin
   S := 'Any old string you want';
   C := S[1];
end.
{--------------------------------------}

Kim Forwood

 

Re:Converting string[1] to CHAR


Quote
In article <4iq88h$...@primus.ac.net> Jack Neely wrote:
>How do you convert a string[1] to a char?

>hne...@ac.net

GIVEN:
VAR s; String[1];
    c: Char;

THEN:
    c := s[1];

Of course if s is empty c is unknown, so the following is safer, but only in
that when s is empty c will have a defined value.

    s := s + #0;
    c := s[1];

                ,,,red

Re:Converting string[1] to CHAR


How do you convert a string[1] to a char?

hne...@ac.net

Re:Converting string[1] to CHAR


Quote
clifp...@airmail.net (CLIF PENN) wrote:
>The following program shows several operations with Char and
>String[1]. The rule to keep in your head is even if a variable is type
>String[1] and has only one character, it is still a string. Therefore

char(string[1]) is a legal method of using a character within a string
as a char.

http://www.webworldinc.com/joejared/index.htm
ftp://webworldinc.com/joejared/
--- Fidoknot v1.0
* Origin: Yorba Linda, CA (joeja...@webworldinc.com) BP8.0 please... Not everyone uses Windoze.

Re:Converting string[1] to CHAR


Quote
In article <4iq88h$...@primus.ac.net>, hne...@ac.net (Jack Neely) wrote:
>How do you convert a string[1] to a char?

Why not try to assign directly?

Sven

Re:Converting string[1] to CHAR


Quote
In article <4iq88h$...@primus.ac.net>, hne...@ac.net (Jack Neely) writes:

|> How do you convert a string[1] to a char?

Let's say you have the declarations:

Var MyStr  : String[1];
    MyChar : Char;

You could as easily assign your string to a char by an instruction like this:

  MyChar := MyStr[1];

Remember that a string is simply an array of characters, so referencing an
element is equivalent to accessing a single char.

You just have to consider here that there really is a character in your string.
If the string is empty (MyStr = ''), then the character you get from MyStr[1] is
garbage.  (It is usually the last character used in the string but this may be
implementation dependent)

So, when converting, it is better to have...

  If (Length(MyStr) = 0) then <Error>
    else MyChar := MyStr[1];

YO!
--
Erwin D. Paguio
http://rh.iist.unu.edu/~ep/ydeeps.html
Pascal and ASM Enthusiast

Re:Converting string[1] to CHAR


Quote
hne...@ac.net (Jack Neely) wrote:
>How do you convert a string[1] to a char?
>hne...@ac.net

The following program shows several operations with Char and
String[1]. The rule to keep in your head is even if a variable is type
String[1] and has only one character, it is still a string. Therefore
only functions and procedures associated with strings will work. The
same applies to variables of type Char. However, you can convert one
to the other without violating either of these rules as shown below.

<clifp...@airmail.net>    3:50PM  3/21/96

The output produced by this program is:

No Crash!   Ch = Z   OneStr = A
Ch = C   OneStr = C
Ch = B   OneStr = B

<<<<Just Press Enter >>>>

---------------------------------------------------------------

Program StrToChar;

CONST  TestChar = 'Z';
       TestStr = 'ABC';

VAR
OneStr:String[1];
Ch:Char;
ThreeStr:String[3];

Begin
Writeln;

Ch := TestChar;
OneStr := TestStr;
Writeln('No Crash!  Ch = ', Ch, '   OneStr = ', OneStr);

OneStr := Copy(TestStr, 3, 1);
Ch := OneStr[1];          (* This works either way, note below *)
Writeln('Ch = ', Ch, '   OneStr = ', OneStr);

ThreeStr := TestStr;
Ch := ThreeStr[2];     (* Ch := TestStr[2] will fail *)
OneStr[1] := Ch;       (* Reverse of above *)
Writeln('Ch = ', Ch, '   OneStr = ', OneStr);

Writeln;
Write('<<<< Just Press Enter >>>>');
Readln;
End.

Re:Converting string[1] to CHAR


Quote
In article <4ism7o$...@www1.hlc.net> joeja...@webworldinc.com "Joe Jared" writes:
> clifp...@airmail.net (CLIF PENN) wrote:

> >The following program shows several operations with Char and
> >String[1]. The rule to keep in your head is even if a variable is type
> >String[1] and has only one character, it is still a string. Therefore

> char(string[1]) is a legal method of using a character within a string
> as a char.

char(string[1]) is a pointless method of using a character within a string
as a char.

The *type* may be string[1], but the expression my_str[1] evaluates to a char

.splitbung
--
* TQ 1.0 * The 'Just So Quotes'.
"Puritanism -- The haunting fear that someone, somewhere, may be happy."
        -- H. L. Mencken

Other Threads