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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:29:36  来源:igfitidea点击:

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

stringdelphichardelphi-xe2case

提问by Japster

I have a string, from which I want to extract one character that is needed to be used in a Casestatement. The thing is the Caseonly takes Charvalues 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 casestatement. 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 stringbecame 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 TStringHelperclass out of unit System.SysUtils:

由于 delphi 成为跨平台,我将使用基于 0 的字符串访问,使用TStringHelperclass out of unit System.SysUtils

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