Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first element in a string?

Tags:

string

delphi

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"

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.

if not(char(myString[0]) in [0..9]) then //Do something

How do I accomplish it?

like image 846
Ivan Prodanov Avatar asked Jul 17 '09 13:07

Ivan Prodanov


People also ask

How do you get the first character in a string JavaScript?

To get the first character of a string, access the string at index 0 , using bracket notation. For example, str[0] returns the first character of the string.

How do I get the first element of a string in C++?

Getting the first character To access the first character of a string, we can use the subscript operator [ ] by passing an index 0 . Note: In C++ Strings are a sequence of characters, so the first character index is 0 and the second character index is 1, etc.

How do you get the first character of a string in Python?

String Indexing Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets ( [] ). String indexing in Python is zero-based: the first character in the string has index 0 , the next has index 1 , and so on.

How do you get the first character of a string in typescript?

charAt() is a method that returns the character from the specified index. Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.


2 Answers

Strings are 1-based:

if not (myString[1] in ['0'..'9']) then // Do something
like image 91
Ondrej Kelle Avatar answered Sep 18 '22 15:09

Ondrej Kelle


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

like image 40
smok1 Avatar answered Sep 18 '22 15:09

smok1