简体   繁体   中英

How to convert one character from a string to a char in delphi

I have a string, from which I want to extract one character that is needed to be used in a Case statement. The thing is the Case only takes Char values and not string values. So how do I convert a single string character to a char?

Use the string as a character array (1-based), and use the index of the character you want to use in the case statement. For instance, if you want to use the first character:

case MyString[1] Of
// ...
end;

NB make sure you check the the string is of at least that length before you use the subscript, or you'll get an access violation.

由于string移动平台上变为0,因此从单字符字符串获取字符串也是一种始终安全的方法。

myString[Low(myString)]

Since delphi became cross-platform I would use 0-based string access using TStringHelper class out of unit System.SysUtils :

case MyString.Chars(0) Of
// ...
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM