ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage functions / Standard functions / String functions
  • Finding a character string
  • Managing ambiguities
  • PositionOccurrence and Unicode
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Finds the Xth position of a string within another string. You can also examine all positions of a substring within a string.
It is also possible to find the Xth position of one of the strings in an array.
Example
n = PositionOccurrence("Antananarivo, Madagascar", "g", 1) // Returns 19
n = PositionOccurrence("Antananarivo, Madagascar", "zen", 1) // Returns 0
n = PositionOccurrence("Antananarivo, Madagascar", "n", 1) // Returns 2
n = PositionOccurrence("Antananarivo, Madagascar", "n", 2) // Returns 5
// Find the third "a" starting from the end: 
// The position is given from the beginning
n = PositionOccurrence("Antananarivo, Madagascar", "a", 3, FromEnd)
// Returns 18
// Find an array of strings
n = PositionOccurrence("Antananarivo, Madagascar", ["an", "ar"], 1) // Returns 4
n = PositionOccurrence("Antananarivo, Madagascar", ["an", "ar"], 2) // Returns 6
n = PositionOccurrence("Antananarivo, Madagascar", ["an", "ar"], 3) // Returns 8
n = PositionOccurrence("Antananarivo, Madagascar", ["an", "ar"], 4) // Returns 23
// Find positions of all the letters "a" and "A"
MyString is string = "Antananarivo, Madagascar"
MyPosition is int = PositionOccurrence(MyString, "a", firstRank, IgnoreCase)
WHILE MyPosition <> 0
	Trace(MyPosition) // Returns 1, 4, 6, 8, 16, 18, 20, 23
	MyPosition = PositionOccurrence(MyString, "a", nextRank, IgnoreCase)
END
// Find positions of all the letters "a", "A", "r" and "R"
MyString is string = "Antananarivo, Madagascar"
MyPosition is int = PositionOccurrence(MyString, ["a", "r"], firstRank, IgnoreCase)
WHILE MyPosition <> 0
	Trace(MyPosition) // Returns 1, 4, 6, 8, 9, 16, 18, 20, 23, 24
	MyPosition = PositionOccurrence(MyString, ["a", "r"], nextRank, IgnoreCase)
END
// Find positions of all the letters "an" and "ar"
MyString is string = "Antananarivo, Madagascar"
MyArray is array of 2 strings
ArrayAdd(MyArray, "an")
ArrayAdd(MyArray, "ar")
MyPosition is int = PositionOccurrence(MyString, MyArray, firstRank, IgnoreCase)
WHILE MyPosition <> 0
	Trace(MyPosition) // Returns 1, 4, 6, 8, 23
	MyPosition = PositionOccurrence(MyString, MyArray, nextRank, IgnoreCase)
END
Syntax

Finding the position of a substring within a string Hide the details

<Result> = PositionOccurrence(<Initial string> , <Search string> , <Index of occurrence to search for> [, <Search options>])
<Result>: Integer
If <Search string> is a character string, the result is the position of the first character of the occurrence to search for in the initial string or 0 if the search string is not found.
If <Search string> is an array of character strings, the result is the position of the first character of the first string found from the array of strings or 0 if no string from the array was found.
<Initial string>: Character string
Character string to be searched (maximum size: 2 GB).
<Search string>: String or Array of strings
This parameter can correspond to:
  • The string to be searched for in the initial string.
  • An array of strings to be searched for in the initial string.
<Index of occurrence to search for>: Integer
Number of the occurrence of <Search string>. <Result> will contain the position of this occurrence.
If this parameter is less than 1, <Result> is set to 0.
If the value of this parameter is greater than the number of characters in <Initial string>, <Result> is equal to 0.
<Search options>: Optional constant
Indicates the search direction and the search options:
FromBeginning
(Default value)
Searches from the first to the last character of the string
FromEndSearches from the last to the first character of the string
IgnoreCaseCase and accent insensitive search (ignores uppercase/lowercase differences)
WholeWordSearches for a whole word (enclosed in punctuation characters or spaces)

Examining the positions of a substring in a string Hide the details

<Result> = PositionOccurrence(<Initial string> , <Search string> , <Browse options> [, <Search options>])
<Result>: Integer
If <Search string> is a character string, the result is the position of the first occurrence (depending on the search direction) in the initial string or 0 when the search has been completed.
If <Search string> is an array of strings, the result is the position of the first character of the previous or next occurrence (depending on the search direction) in the array of strings or 0 if the search has been completed.
<Initial string>: Character string
Character string to be searched (maximum size: 2 GB).
<Search string>: String or Array of strings
This parameter can correspond to:
  • The string to be searched for in the initial string.
  • An array of strings to be searched for in the initial string.
<Browse options>: Integer constant
Browse direction:
firstRankStarts searching for substrings separated by the specified <Search string> from the beginning of the string.
lastRankStarts searching for substrings separated by the specified <Search string> from the end of the string.
nextRankContinues the search started with firstRank
previousRankContinues the search started with lastRank
<Search options>: Optional constant
Indicates the search direction and the search options:
FromBeginning
(Default value)
Searches from the first to the last character of the string
FromEndSearches from the last to the first character of the string
IgnoreCaseCase insensitive search (ignores uppercase/lowercase differences).
WholeWordSearches for a whole word (enclosed in punctuation characters or spaces)
Remarks

Finding a character string

By default, the search is case sensitive: the searched string must have the same case as the string to be found in the string.. To perform a case-insensitive search, use the IgnoreCase constant.

Managing ambiguities

When the search strings are substrings of other strings in the array, the longest strings are taken into account.
Example: If the string array ["bon", "bonjour", "bonsoir "] is to be searched in "Bien le bonjour mon bon monsieur", the first occurrence found will be "bonjour" (not "bon").
Reports and Queries

PositionOccurrence and Unicode

<Initial string> and <Search string> can both correspond to:
  • ANSI strings.
  • or Unicode strings.
  • buffers.
You have the ability to use ANSI strings, Unicode strings and buffers in the different parameters of the function.
The following conversion rule is used for the Ansi systems (Windows or Linux):
  • If at least one of the strings is a buffer, all the strings are converted to buffers and the operation is performed with buffers.
  • If the first condition is not met and there is at least one Unicode string, all the strings are converted to Unicode and the operation is performed in Unicode (the conversion is performed with the current character set, if necessary).
  • Otherwise, the operation is performed in Ansi.
The conversion rule used for Unicode systems is as follows:
  • If at least one of the strings is a buffer, all the strings are converted to buffers and the operation is performed with buffers.
  • Otherwise, the operation is performed in Unicode.
For more details on Unicode, see Unicode management.
Remember: the language parameters used are defined when the ChangeCharset function is called.
Component: wd300vm.dll
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 10/17/2024

Send a report | Local help