devxlogo

Selecting and Moving Parts of a String

Selecting and Moving Parts of a String

Question:
I have two TField400s. The first TField400 contains the value “12345678” and other TField400 contains the value “11111111”. I want to move the “5678” from the first TField400 and place it in the second TField400 so the value in the second TField400 will be “11115678” instead of “11111111”.

Answer:
To accomplish this task, just use the Copy function for handling strings.

//Grab the string field valuesS1 := MyTable.FieldByName('Field1').AsString;S2 := MyTable.FieldByName('Field2').AsString;//Now copy the first four bytes of Field2 and concatenate the second four bytes of Field1 to it.MyTable.FieldByName('Field2').AsString := Copy(S2, 1, 4) + Copy(S1, 5, 4);

Copy takes three parameters. The first is a string value, the second is the index to start copying from, and the third is the number of bytes to copy.

devx-admin

Share the Post: