string 如何获取字符串中的第一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1143340/
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 get the first element in a string?
提问by Ivan Prodanov
I'm trying to figure out a way to check a string's first element if it's either a number or not.
我试图找出一种方法来检查字符串的第一个元素是否为数字。
if not(myString[0] in [0..9]) then //Do something
The problem is that I get an error "Element 0 inaccessible - use 'Length' or 'SetLength"
问题是我收到错误“元素 0 无法访问 - 使用‘长度’或‘设置长度’”
Another way came to my head from my C-like exprieince - convert the first element of the string to char and check the char,but there is no difference in the compile errors.
从我的类似 C 的经验中,我想到了另一种方法 - 将字符串的第一个元素转换为 char 并检查 char,但编译错误没有区别。
if not(char(myString[0]) in [0..9]) then //Do something
How do I accomplish it?
我该如何实现?
回答by Ondrej Kelle
Strings are 1-based:
字符串是基于 1 的:
if not (myString[1] in ['0'..'9']) then // Do something
回答by smok1
Pascal and Delphi indexes string from 1. This is a legacy from time where zero byte contained length, while next 255 (index 1 to 255) contained actual characters.
Joel Spolsky wrote quite good article on string issues:
http://www.joelonsoftware.com/articles/fog0000000319.html
Pascal 和 Delphi 从 1 开始索引字符串。这是从零字节包含长度而下一个 255(索引 1 到 255)包含实际字符的时代遗留下来的。Joel Spolsky 在字符串问题上写了相当不错的文章:http:
//www.joelonsoftware.com/articles/fog0000000319.html
回答by Oliver Giesen
Delphi strings use a 1-based index, so just rewrite to
Delphi 字符串使用基于 1 的索引,因此只需重写为
if not(myString[1] in ['0'..'9']) then //Do something
Also take note of the quotes around the 0..9
, otherwise you would be comparing characters to integers.
还要注意 周围的引号0..9
,否则您会将字符与整数进行比较。
回答by Andrei Galatyn
We should keep in mind some things:
我们应该记住一些事情:
- String in Delphi is 0-based for mobile platforms and 1-based for Windows.
- String in old versions of Delphi is
AnsiString
(1-byte per char) andWideString
in new versions (2-bytes per char). - Delphi supports set of
AnsiChar
, but doesn't support set ofWideChar
.
- Delphi 中的字符串对于移动平台是基于 0 的,对于 Windows 是基于 1 的。
- 旧版本的 Delphi 中的字符串是
AnsiString
(每个字符 1 个字节)和WideString
新版本中的(每个字符 2 个字节)。 - Delphi 支持 set of
AnsiChar
,但不支持 set ofWideChar
。
So if we want to write a code compatible with all versions of Delphi, then it should be something like this:
所以如果我们想写一个兼容所有Delphi版本的代码,那么应该是这样的:
if (myString[Low(myString)]>='0') and (myString[Low(myString)]<='9') then
// Do something
回答by Chester
With later updates to Delphi mobile code, the bottom string index changed from 0 to 1. When you compile older programmes, they compile and run correctly using 0 starting index. Programmes created with the later IDE produce an error. When you have mixtures, life gets complex!
随着 Delphi 移动代码的后续更新,底部字符串索引从 0 更改为 1。当您编译旧程序时,它们使用 0 起始索引正确编译和运行。使用较新的 IDE 创建的程序会产生错误。当你有混合物时,生活就会变得复杂!
It would be good to be able to take an older programme and tell the IDE that you want it brought up to date (maybe this would fix other things, like fonts getting scrambled when you answer a phone call!) but it would be good to get things consistent!
能够使用较旧的程序并告诉 IDE 您希望它是最新的(也许这可以解决其他问题,例如在您接听电话时字体变得混乱!)让事情保持一致!
回答by Bruce McGee
if not(myString[0] in [0..9]) then //Do something
if not(myString[0] in [0..9]) then //做点什么
If you're using Delphi 2009, the TCharacter class in Character.pas has functions like IsDigit to help simplify these kinds of operations.
如果您使用的是 Delphi 2009,Character.pas 中的 TCharacter 类具有像 IsDigit 这样的函数来帮助简化这些类型的操作。
Once you fix the indexing, of course. :)
当然,一旦你修复了索引。:)
回答by skamradt
The simplest way to check to see if the first character of string is an integer, and then dispatch:
检查字符串的第一个字符是否为整数的最简单方法,然后调度:
var
iResult : integer;
begin
if TryStrToInt( mySTring[1], iResult) then
begin
// handle number logic here iResult = number
end
else
begin
// handle non number logic here
end;
end;
回答by Ken White
I use a utility function to test the entire string:
我使用一个实用函数来测试整个字符串:
function IsNumeric(const Value: string): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(Value) do
if not (Value[i] in ['0'..'9','.','+','-']) then
begin
Result := False;
Break;
end;
end;
The above code is for Delphi versions prior to 2007. In 2007 and 2009, you could change the integer variable i to a character c, and use for c in Value instead.
上述代码适用于2007年之前的Delphi版本。在2007年和2009年,您可以将整数变量i更改为字符c,并在Value中使用for c。
To test for integers only, remove the '.' from the set of characters to test against.
要仅测试整数,请删除“.” 来自要测试的字符集。
回答by Marco van de Voort
This is incorrect. ISO strings and older Pascal's also started at one. It is just a general convention, and afaik the s[0] thing is a result of that being vacant, and cheap to code in the UCSD bytecode interpreter. But that last bit is before my time, so only my guessing.
这是不正确的。ISO 字符串和较旧的 Pascal 字符串也是从一开始的。这只是一个通用约定,而 s[0] 是空置的结果,并且在 UCSD 字节码解释器中编码很便宜。但最后一点是在我的时间之前,所以只是我的猜测。
It results from the Pascal ability to have arbitrary upper and lower bounds, which provides for more typesafety accessing arrays.
它源于 Pascal 具有任意上限和下限的能力,这为访问数组提供了更多类型安全性。
Really old Pascal strings (till early eighties) strings were even worse than C ones btw. Multiple conventions were in used, but all were based on static arrays (like early C), but they were typically space padded, so you had scan back from the end till the spaces ended.
非常古老的 Pascal 弦(直到 80 年代初)甚至比 C 弦还要糟糕。使用了多种约定,但所有约定都基于静态数组(如早期的 C),但它们通常是空格填充的,因此您必须从末尾扫描到空格结束。
(removed the legacy tag, since being 1 based is not legacy. Accessing s[0] as length IS legacy, but that is not what the question is about)
(删除了遗留标签,因为基于 1 不是遗留的。访问 s[0] 作为长度是遗留的,但这不是问题所在)
回答by nickson
Foreach element in strName
if not element in [0-9] then
do something
else
element is a digit
end if
Don't forget the quote between digits number.
不要忘记数字之间的引号。