User: alciro    User
 

Programación en C++ Builder

Share |
 Arrays (matrices)
 Punteros
3. Ejemplo de clase en c++
8. Métodos de la clase AnsiString
 Proyectos en C++
 Packages, distribuir una aplicación sin instalación
 Ordenación por intercambio o burbuja
 Funciones de cadenas de caracteres string.h

10. La clase TStrings

La clase TString para la manipulación de cadenas de caracteres.

10.1. Propiedades

Capacity
Indica el número de cadenas que la clase Tstrings puede contener.

__property int Capacity = {read=GetCapacity, write=SetCapacity, nodefault};

Description

Read Capacity to determine the currently allocated size of the string list. For the TStrings object, reading Capacity returns the Count property, and setting Capacity does nothing. Descendants of TStrings can override this property to allow a string list to allocate memory for entries that have not been added to the list.

CommaText
Lista las cadenas contenidas en el objeto TStrings con el sistema de formato de datos (SDF).
Entrega una cadena de todas la líneas separadas por comas.

__property System::AnsiString CommaText = {read=GetCommaText, write=SetCommaText};

Description

Use CommaText to get or set all the strings in the TStrings object in a single comma-delimited string.

When retrieving CommaText, any string in the list that include spaces, commas or quotes will be contained in double quotes, and any double quotes in a string will be repeated. For example, if the list contains the following strings:

Stri,ng 1

Stri"ng 2
String 3
String4

CommaText will return:

"Stri,ng 1","Stri""ng 2","String 3",String4

When assigning CommaText, the value is parsed as SDF formatted text. For SDF format, strings are separated by commas or spaces, and optionally enclosed in double quotes. Double quote marks that are part of the string are repeated to distinguish them from the quotes that surround the string. Spaces and commas that are not contained within double quote marks are delimiters. Two commas next to each other will indicate an empty string, but spaces that appear next to another delimiter are ignored. For example, suppose CommaText is set to:

"Stri,ng 1", "Stri""ng 2" , String 3,String4

The list will then contain:

Stri,ng 1

Stri"ng 2
String
3
String4

Count
Indica el número de cadenas en la lista.

__property int Count = {read=GetCount, nodefault};

Description

Descendants of TStrings implement a Count property to indicate the number of strings in the list.

Use the Count property when iterating over all the strings in the list, or when trying to locate the position of a string relative to the last string in the list.

The following example updates the strings in a list box given the strings contained in another list box. If a string in the source list box has the form Name=Value and the destination list box contains a string with the same Name part, the Value part in the destination list box will be replaced by the source's value.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *stream;
char FirstLine[512];

OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
OpenDialog1->FilterIndex = 2; // start the dialog showing all files
if (OpenDialog1->Execute())
{
for (int I = 0; I < OpenDialog1->Files->Count; I ++)
{
stream = fopen(OpenDialog1->Files->Strings[I].c_str(), "r");

if (stream)
{
// read the first line from the file
fgets(FirstLine, sizeof(FirstLine), stream);
Memo1->Lines->Append(FirstLine);
fclose(stream);
}
}
}
}

Names
Objects
Strings
Hace referencia a la cadena en la lista, en la posición establecida.

Ejemplo:

Memo1->Lines->Strings[0]="Primera cadena";

__property System::AnsiString Strings[int Index] = {read=Get, write=Put};

Description

Descendants of TStrings must implement an accessor function for the Strings property to return the string at the position indicated by Index. Index gives the position of the string, where 0 is the first string, 1 is the second string, and so on.

Use the Strings property to get or set the string at a particular position.

This example uses an Open dialog box, a memo, and a button on a form. When the user clicks the button, the Open dialog box appears. When the user selects files in the dialog box and chooses the OK button, the first line from each of the files is added to the memo.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *stream;
char FirstLine[512];

OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
OpenDialog1->FilterIndex = 2; // start the dialog showing all files
if (OpenDialog1->Execute())
{
for (int I = 0; I < OpenDialog1->Files->Count; I ++)
{
stream = fopen(OpenDialog1->Files->Strings[I].c_str(), "r");

if (stream)
{
// read the first line from the file
fgets(FirstLine, sizeof(FirstLine), stream);
Memo1->Lines->Append(FirstLine);
fclose(stream);
}
}
}
}

StringsAdapter
Text
Lista las cadenas contenidas en el objeto TStrings en una cadena única con las cadenas individuales delimitadas por un retorno de carro y un avance de línea.
Para tratar todas las líneas como una única cadena de texto AnsiString.

__property System::AnsiString Text = {read=GetTextStr, write=SetTextStr};

Description

Use Text to get or set all the strings in the TStrings object in a single string delimited by carriage return, line feed pairs.

When reading Text, the strings in the list will be separated by carriage return, line feed pairs. If any of the strings in the list contain a carriage return and line feed pair, the resulting value of Text will appear to contain more strings than is indicated by the Count property.

When setting Text, the value will be parsed by separating into substrings whenever a carriage return or linefeed is encountered. (The two do not need to form pairs).

If the strings in the list contain carriage return or linefeed characters, a less ambiguous format for the strings is available through the CommaText property.

Ejemplo:

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
Memo1->Lines->Text=Memo1->Lines->Text+Key;
}

Values

 

Loading

copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |