Solved

Converting a string to an Array

Posted on 2000-04-25
7
206 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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