¿Hay una forma simple en delphi para convertir una matriz de cadenas en una tstringlist?delphi array of string stringlist conversion
Respuesta
Una vez que haya creado la lista de cadenas, puede simplemente llamar al AddStrings()
.
O para las versiones anteriores de Delphi que no son compatibles con las sobrecargas AddStrings()
que aceptan matrices, puede hacer las suyas propias.
function StringListFromStrings(const Strings: array of string): TStringList;
var
i: Integer;
begin
Result := TStringList.Create;
for i := low(Strings) to high(Strings) do
Result.Add(Strings[i]);
end;
El uso de un parámetro de matriz abierta ofrece la máxima flexibilidad para la persona que llama.
Para las versiones pre-genéricos de Delphi, se puede usar algo como esto:
type
TStringArray = array of string;
procedure StringListFromStrings(const StringArray: TStringArray;
const SL: TStringList);
var
// Versions of Delphi supporting for..in loops
s: string;
// Pre for..in version
// i: Integer;
begin
// TStringList should be created and passed in, so it's clear
// where it should be free'd.
Assert(Assigned(SL));
// Delphi versions with for..in support
for s in StringArray do
SL.Add(s);
// Pre for..in versions
// for i := Low(StringArray) to High(StringArray) do
// SL.Add(StringArray[i]);
end;
+1 para el crujiente-viejo-delphi-versions-love. –
@Warren: Gracias. Algunos de nosotros estamos atascados usándolos porque tenemos proyectos que no necesitan soporte Unicode (en las aplicaciones internas en particular) y por lo tanto no pueden justificar el trabajo para convertirlos a nuevas versiones de Delphi (y no pueden justificar el gasto de las nuevas versiones en sí mismas porque a los futuros no les importan los Unicode o los genéricos, etc.). –
Entiendo totalmente. :-) –
- 1. numpy array of chars to string
- 2. Delphi Conversion Unicode Issues
- 3. Safe String to BigDecimal conversion
- 4. Java array of Hashtables
- 5. Delphi: SetLength() en el argumento de tipo "array of TObject"
- 6. Exploding by Array of Delimiters
- 7. Android Resource - Array of Arrays
- 8. sum of array column
- 9. numpy array of objects
- 10. Array of PHP Objects
- 11. Split String en String array
- 12. Android string-array a Array
- 13. VB.NET ArrayList a List (Of T) copy/conversion mecanografiado
- 14. Weighted Centroid of a Array
- 15. C - initialize array of structs
- 16. Sort ArrayList of Array en Java
- 17. GSON Deserializing Array of Custom Objects
- 18. C - unsigned int a unsigned char array conversion
- 19. Word Array to String
- 20. JSONArray a string array
- 21. object to string array
- 22. C++ get index of element of array por valor
- 23. ¿Qué pasa con: \t LinkedList <String> stringList = new LinkedList <String>();
- 24. Delphi: array JSON
- 25. PHP Santizing/Validating Array of Integers
- 26. Array of Strings en Fortran 77
- 27. Generación de WSDL con NuSOAP - return struct con varios tipos (int, string, array of structs)
- 28. PHP Array to String equivalent
- 29. String to Char array Java
- 30. Imprimir una cadena de ArrayList of String []?
perfecta, gracias! :) – xianghua
Tenga en cuenta que esto solo funciona en D2009 en adelante (donde los genéricos están disponibles). El mismo código de tipo en D2007 (que admite la sintaxis del constructor de matriz dinámica) falla con "E2010 Tipos incompatibles: 'TStrings' y 'TStringArray'", donde 'escriba TStringArray = array of string', y se utiliza como' SA: = TStringArray. Create ('Uno', 'Dos', Tres '); 'y el TStringList.AddStrings se llama' SL.AddStrings (SA); ' –
La última actualización funciona bien en todas las versiones de Delphi, incluso aquellas que no lo hacen tener matrices dinámicas (!) –