ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage syntax / Structured statements
  • Syntax 1: Iterating over substrings separated by a separator
  • Syntaxes 2 and 3: Iterating over the occurrences of a string inside another string
  • FOR ANY CHAIN, FOR ANY POSITION and Unicode
  • Tip: How to get the separator?
  • Tip: Iterating over an XML string
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
The FOR EACH statement is used to browse strings according to different methods:
  • Browsing the substrings separated by a separator.
  • Finding the occurrences of a string inside another string.
Note: The FOR EACH, FOR ALL statements are accepted. The FOR EACH statement will be used in this documentation but it can be replaced with FOR ALL.
The FOR EACH statement can also be used to browse the .Net objects that implement the IEnumerable interface.
Example
// Récupère la liste des librairies chargées en mémoire
ListeLibrairie is string = ListDLL()

// Pour chaque librairie
FOR EACH STRING UneLibrairie OF ListeLibrairie SEPARATED BY CR
	// Ajoute la librairie dans la table TABLE_Librairie
	TableAddLine(TABLE_Librairie, ExtractString(UneLibrairie, 1, TAB))
END
// Le fichier "C:\MesDocuments\Exports.TXT" contient la liste
// des produits exportés séparés par des ";"
// Récupération de chaque produit
ProduitExport is string = fLoadText("C:\MesDocuments\Exports.TXT")
AnciennePosition is int

// Pour chaque produit
FOR EACH POSITION PositionActuelle OF ";" IN ProduitExport
	// Ajoute le produit dans la liste ListeProduit
	ListAdd(LISTE_Produit, ...
		ProduitExport[[AnciennePosition + 1 TO PositionActuelle - 1]]
	// Mémorisation de la position
	AnciennePosition = PositionActuelle 
END
FOR EACH STRING sc1, nPosition, nCompteur OF "A.B.C" SEPARATED BY "."
	Trace(sc1 + " - " + nPosition + " - " + nCompteur)
END
// Renvoie
// A - 1 - 1
// B - 3 - 2
// C - 5 - 3
s is string = "je suis une phrase"+CR+"sur plusieurs lignes"
FOR EACH STRING s2 OF s SEPARATED BY [" ",CR]
	Trace(s2)
END

// Renvoie
// je
// suis
// une
// phrase
// sur
// plusieurs
// lignes
Syntax

Browsing the substrings separated by a separator Hide the details

FOR EACH STRING <Substring> [, <Position> [, <Counter>]] OF <Initial string>
                                                                   [SEPARATED BY <Separator>] [<Direction>]

    ...
END
<FOR EACH STRING>:
Marks the beginning of the statement block.
<Substring>:
String variable containing the text of the substring. There is no need to declare this variable.
<Position>:
Integer variable containing the position of the substring in the string. There is no need to declare this variable.
<Counter>:
Integer variable containing the number of iterations. There is no need to declare this variable.
<Initial string>:
String containing the full text. The iteration does not take place if this string is empty.
<Separator>:
Optional string containing the separator of substrings (TAB by default).
To specify several separators, use the following syntax:
[<Separator1> , ..., <Separator N>]
For example: [TAB, CR]
<Direction>:
Optional indicator for the iteration direction:
FromBeginning
(default value)
Browse the string from the first character to the last one.
FromEndBrowse the string from the last character to the first one.

Browsing the occurrences of a string inside another string Hide the details

FOR EACH POSITION <Position> OF <Search> IN <Initial String> [<Direction>]

    ...
END
<FOR EACH POSITION>:
Marks the beginning of the statement block.
<Position>:
Integer variable containing the current position. There is no need to declare this variable.
<Search>:
Sought string.
<Initial string>:
String containing the full text.
<Direction>:
Optional indicator for the iteration direction:
FromBeginning
(default value)
Browse the string from the first character to the last one.
FromEndBrowse the string from the last character to the first one.

Browsing the occurrences of a string inside another string Hide the details

FOR EACH POSITION <Position> OF <Search> IN <Initial String> WITH <Options>

    ...
END
<FOR EACH POSITION>:
Marks the beginning of the statement block.
<Position>:
Integer variable containing the current position. There is no need to declare this variable.
<Search>:
Sought string.
<Initial string>:
String containing the full text.
<Options>:
Indicator for the selected options (can be combined):
FromBeginning
(default value)
Browse the string from the first character to the last one.
FromEndBrowse the string from the last character to the first one.
WholeWordSearch for the whole word
IgnoreCaseCase-insensitive search (upper/lowercase)
Remarks

Syntax 1: Iterating over substrings separated by a separator

Browses all the substrings in <Initial string> separated by <Separator>. The browse is not performed if <Initial String> is an empty string.
For each iteration:
  • <Substring> is filled with the current substring.
  • <Position> contains the position of the substring inside the string.
  • <Counter> contains the number of iterations performed.
The behavior is undefined if the initial string or the separator is modified during the browse.
Note: If <Initial string> ends with the separator, <Substring> returns an empty string at the end. Otherwise, <Substring> corresponds to the last checked-out element.

Syntaxes 2 and 3: Iterating over the occurrences of a string inside another string

Finds all the occurrences of the <Search> substring in the <Initial string>.
At each iteration, the position of the current substring is assigned to the <Position> variable.
The behavior is undefined if the initial string or the sought string is modified during the browse.

FOR ANY CHAIN, FOR ANY POSITION and Unicode

<Substring>, <Initial string>, <Separator> and <Search> can correspond to:
  • ANSI strings.
  • or Unicode strings.
However, it is not possible to use ANSI and Unicode strings in the same syntax.
For more details, see Unicode management.

Tip: How to get the separator?

To retrieve the separator in the loop, use the following syntax. To get the separator, simply retrieve the following character.
For example:
FOR EACH STRING sChaineTemp, nPosition OF sChaineIN SEPARATED BY [" ","-"]
	sSeparateur = sChaineIN[[nPosition + Length(sChaineTemp)]]
	...
END

Tip: Iterating over an XML string

To iterate over an XML file, call XMLExecuteXPath before using FOR EACH.
Minimum version required
  • Version 10
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/20/2025

Send a report | Local help