slecault
14 years agoContributor
How to loop for each line of a multi-line string
I need to split a text file into many small ones. My source text files are in the range of 1500 to 8000 lines.
The source file contains a list of controls with all their properties (this file is not generated within my code)
exemple:
...
[Control1]
Caption:'hello'
With:12
Height:22
[Control333]
Caption:'hello'
With:12
Height:22
From the above, I need to create a file for Control1 containing all the lines until Control333 is reached, then create another file...
What I have got so far is to read the file line by line with ReadLn, analyse the content, cumulate the lines and write the cumulated lines.
I saw that I could ReadAll lines at once.
My question, if all my text source lines are in a single variable, how can I loop trough this variable line by line?
The source file contains a list of controls with all their properties (this file is not generated within my code)
exemple:
...
[Control1]
Caption:'hello'
With:12
Height:22
[Control333]
Caption:'hello'
With:12
Height:22
From the above, I need to create a file for Control1 containing all the lines until Control333 is reached, then create another file...
What I have got so far is to read the file line by line with ReadLn, analyse the content, cumulate the lines and write the cumulated lines.
I saw that I could ReadAll lines at once.
My question, if all my text source lines are in a single variable, how can I loop trough this variable line by line?
- Hi Stephane,
Basically, you need to split the string by the new line character and then iterate through individual items in the resulting array. For example:var arr = str.split("\r\n");
for (var i = 0; i < arr.length; i++)
{
// Do something with arr
}
You can also loop through the text using TestComplete's aqString.GetListItem object if you set aqString.ListSeparator to the new line character ("\r\n" in JScript, vbNewLine constant in VBScript or #13#10 in DelphiScript). Have a look at examples in the linked topics.
By the way, your file looks similar to an INI file, only with a non-standard separator for name-value pairs.
If you could modify your file into:
; NB: The Root section is mandatory
[Root]
[Control1]
Caption='hello'
With=12
Height=22
[Control333]
Caption='hello'
With=12
Height=22
you'd be able to access the sections and name-value pairs using TestComplete's Storages.INI API, instead of manually parsing the file.