Lainkes 0 Posted October 5, 2022 Hello, I have this code procedure Tfrm_langauge.Button1Click(Sender: TObject); var I: Integer; dirtystr, cleanstr : string; begin cleanstr := ''; dirtystr := '123456'; for I := 0 to dirtystr.Length - 1 do cleanstr := cleanstr + dirtystr[I]; end; For some reason, the cleanstr string is always empty. Any idea what is going wrong. I'm sure it's something stupid. The purpose is to remove all characters except numbers for a string. I check the character and if it is a number, I add it to my new string. When I tested that function, it returned always an empty string. That's why I did this little test with the above code, with the same result. Thanks for your help Lainkes Share this post Link to post
Uwe Raabe 2057 Posted October 5, 2022 Run the for-loop from 1 to length. 1 Share this post Link to post
Lainkes 0 Posted October 5, 2022 Thanks. This worked. I thought that a string was an array of characters. And an array starts at position 0. Share this post Link to post
FPiette 383 Posted October 5, 2022 24 minutes ago, Lainkes said: I thought that a string was an array of characters. Not at all ! Share this post Link to post
David Heffernan 2345 Posted October 5, 2022 56 minutes ago, FPiette said: Not at all ! In Delphi a string is essentially a 1 based array of char Share this post Link to post
FPiette 383 Posted October 5, 2022 9 minutes ago, David Heffernan said: 1 hour ago, FPiette said: Not at all ! In Delphi a string is essentially a 1 based array of char Not really and I know you know it. It looks much like a 1 based array of char but it isn't at all compiler magic happens here. A string is a record with data used by the compiler a pointer to the actual characters, a count of characters (length), a reference count and more. Share this post Link to post
David Heffernan 2345 Posted October 5, 2022 (edited) 7 minutes ago, FPiette said: Not really and I know you know it. It looks much like a 1 based array of char but it isn't at all compiler magic happens here. A string is a record with data used by the compiler a pointer to the actual characters, a count of characters (length), a reference count and more. No, viewing a string as an array of char is pretty reasonable. I mean your argument could also be used to say that TArray<Integer> is not an array of integer because of the meta data. And when you do PChar(someString) to pass a null-terminated array of char to some external library, the compiler just passes the address of the first element. (Yes, I know about the empty string special case) Edited October 5, 2022 by David Heffernan Share this post Link to post
FPiette 383 Posted October 6, 2022 13 hours ago, David Heffernan said: No, viewing a string as an array of char is pretty reasonable. They key word is "Viewing". That's what I said: compiler magic makes that happens a string is not an array but sometimes looks like an array of char because of the [ ] that is usable. The two types string and array of char are not interchangeable. You will confuse the OP pretending they are the same. Share this post Link to post
David Heffernan 2345 Posted October 6, 2022 46 minutes ago, FPiette said: The two types string and array of char are not interchangeable. You will confuse the OP pretending they are the same. I didn't say they were interchangeable. A Delphi string is an array of elements of type Char. The documentation says: "A string represents a sequence of characters." Wikipedia defines an array here: https://en.m.wikipedia.org/wiki/Array_(data_structure) So I guess perhaps what may be confusing here is maybe you think I am saying that a Delphi string is a Delphi dynamic array, TArray<Char>. I'm not saying that. I'm using the general computing usage of the word array. Share this post Link to post
FPiette 383 Posted October 6, 2022 7 hours ago, David Heffernan said: I didn't say they were interchangeable. A Delphi string is an array of elements of type Char. You said: " In Delphi a string is essentially a 1 based array of char". No matter how you try to say it is it doesn't make it happens. A Delphi string is NOT an array of elements of type char. If the were, they would be interchangeable and you admitted they are not. Share this post Link to post
David Heffernan 2345 Posted October 6, 2022 30 minutes ago, FPiette said: No matter how you try to say it is it doesn't make it happens. A Delphi string is NOT an array of elements of type char. If the were, they would be interchangeable and you admitted they are not. You can understand what I wrote that way if you want. But as I explained I never intended the meaning to be that a string is the same as the Delphi type array of Char. As I explained I intended to use the word array in its general sense. For instance an array in C# is not assignment compatible with a Delphi array. Does that make a C# array not an array? Share this post Link to post
FPiette 383 Posted October 6, 2022 48 minutes ago, David Heffernan said: You can understand what I wrote that way if you want. Yes I do. Share this post Link to post