ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage syntax / Structured statements
  • Syntax 1: Browsing the substrings separated by a separator
  • Syntax 2 and 3: Finding the occurrences of a string inside another string
  • FOR EACH STRING, FOR EACH POSITION and UNICODE
  • Tip: How to get the separator?
  • Tip: Browsing an XML string
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
The FOR EACH statement is used to browse strings according to different methods:
  • Browsing the substrings separated by a separator.
  • Browsing the occurrences of a string inside another one.
Remark: The FOR ALL, FOR EACH 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
// Retrieves the list of libraries loaded in memory
LibraryList is string = ListDLL()
 
// For each library
FOR EACH STRING ALibrary OF LibraryList SEPARATED BY CR
// Adds the library into the TABLE_Library table
TableAddLine(TABLE_Library, ExtractString(ALibrary, 1, TAB))
END
// The "C:\MyDocuments\Exports.TXT" file contains the list
// of exported products, separated by ";"
// Retrieve each product
ExportedProduct is string = fLoadText("C:\MyDocuments\Exports.TXT")
FormerPosition is int
 
// For each product
FOR EACH POSITION CurrentPosition OF ";" IN ExportedProduct
// Adds the product into ProductList
ListAdd(LIST_Product, ...
ExportedProduct[[FormerPosition + 1 TO CurrentPosition - 1]]
// Store the position
FormerPosition = CurrentPosition
END
FOR EACH STRING sc1, nPosition, nCounter OF "A.B.C" SEPARATED BY "."
Trace(sc1 + " - " + nPosition + " - " + nCounter)
END
// Returns
// A - 1 - 1
// B - 3 - 2
// C - 5 - 3
s is string = "I am a sentence"+CR+"on several lines"
FOR EACH STRING s2 OF s SEPARATED BY [" ",CR]
Trace(s2)
END
 
// Returns
// I
// am
// a
// sentence
// on
// several
// lines
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: Browsing the 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.
Remark: If <Initial string> ends with the separator, <Substring> returns an empty string at the end. Otherwise, <Substring> corresponds to the last checked-out element.

Syntax 2 and 3: Finding 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 EACH STRING, FOR EACH POSITION and UNICODE

<Substring>, <Initial string>, <Separator> and <Search> can correspond to:
  • ANSI strings.
  • UNICODE strings.
However, the ANSI strings and the UNICODE strings cannot be used in the same syntax.
For more details, see Managing UNICODE.

Tip: How to get the separator?

To retrieve the separator in the loop, use the following syntax that allows you to find out the position of the separator. To find out the separator, all you have to do is retrieve the following character.
For example:
FOR EACH STRING sTempString, nPosition OF sStringIN SEPARATED BY [" ","-"]
sSeparator = sStringIN[[nPosition + Length(sTempString)]]
...
END

Tip: Browsing 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: 07/03/2023

Send a report | Local help