Solved

Converting a string to an Array

Posted on 2000-04-25
7
203 Views
Last Modified: 2010-04-04
I'm Pulling information from a DataBase in the form of a string.  I need to know how to convert the data into an array.

D := Table1.FieldByName ('drives').AsString;

I have an array defined
MyArray : array[0..15] of char;
when I try to assign D to MyArray I keep getting incompatible types
'Array' 'String'  
MyArray := D;

I need to look at the data from the database one Character at a time.
0
Comment
Question by:msmouse
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2747126
Aside from the possibility to access each character separately by using D[x] you can do:

  PChar(@MayArray[0]) := Table1.FieldByName ('drives').AsString;

or

  Move(D[1], MyArray, Length(D));

Ciao, Mike
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2747134
Sorry, only the second solution works. Casting the array to anything does only produce the well known "left side cannot be assigned to" error.

Ciao, Mike
0
 
LVL 1

Expert Comment

by:msedi
ID: 2747183
Hello,

First, why don't you use a dynamic array. With the array you use, you are always fixed to a specific size. So try
 
  var
    MyArray : array of Char;

instead of your first thing. Then set the length of the array with SetLength(MyArray, e.g. 15). That's the first thing. You can access the array as usual.

After that you can get the value of your database with

var
  D : string;

begin  
  D := Table1.FieldByName      ('drives').AsString;

  if (D <> '') then
  begin
    MyArray[0] := D[1]; // First element in string
  end
  else MyArray := #0;
end;
   
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:Lischke
ID: 2747217
Hi msedi,

welcome on E-E :-) I want to let you know that proposing an answer is considered bad behaviour and against the rules here if someone else (in this case me :-)) has already commented to the question. Of course, if you are very sure you have the solution and all other just have given nonsense then proposing an answer is acceptable. But this usually happens very seldom and when using only comments then the questioneer can pick one and promote it to being an answer. Additionally, it happend quite often (also to me) that the proposed answer has been accepted accidently although another one was meant, because the questioneer thought the answer came from the same person as the previous comments.

Thank you and

Ciao, Mike
0
 
LVL 1

Expert Comment

by:msedi
ID: 2747561
Sorry,

I didn't see that.

Greetings, Martin
0
 
LVL 1

Author Comment

by:msmouse
ID: 2750198
msedi,  I tried your way but you get 'dynamic Array' 'char' error.

Lischke  The move function seems to be a step in the right direction, I'm not getting the types error.

0
 
LVL 1

Author Comment

by:msmouse
ID: 2750201
A step in the right direction, Now I just have to work with getting the array to do what I want.

Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now