string delphi 如何将一个字符从字符串转换为字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10853675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert one character from a string to a char in delphi
提问by Japster
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?
我有一个字符串,我想从中提取一个需要在Case
语句中使用的字符。事情是Case
唯一的Char
值而不是字符串值。那么如何将单个字符串字符转换为字符呢?
回答by Tony Hopkinson
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:
将字符串用作字符数组(从 1 开始),并使用要在case
语句中使用的字符的索引。例如,如果您想使用第一个字符:
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.
注意,在使用下标之前,请确保检查字符串的长度至少为该长度,否则会出现访问冲突。
回答by Jacek Krawczyk
Since string
became 0-based on mobile platforms, there is also an always-safe way to get a char from single-character string.
由于string
在移动平台上成为 0-based ,还有一种始终安全的方法可以从单字符串中获取字符。
myString[Low(myString)]
回答by BitBumper
Since delphi became cross-platform I would use 0-based string access using TStringHelper
class out of unit System.SysUtils
:
由于 delphi 成为跨平台,我将使用基于 0 的字符串访问,使用TStringHelper
class out of unit System.SysUtils
:
case MyString.Chars(0) Of
// ...
end;