ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / String functions
  • Finding a character string
  • Managing ambiguities
  • PositionOccurrence and UNICODE
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
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 where the search will be performed (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 (between 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 where the search will be performed (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 (between punctuation characters or spaces)
Remarks

Finding a character string

By default, the search is case-sensitive: the search string must have the same case as the substring to find 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 ["after", "afternoon", "afterward"] array of strings must be found in "Come this afternoon after the game", the first occurrence found will be "afternoon" (and not "after").
WINDEVWEBDEV - Server codeReports and QueriesUniversal Windows 10 AppAndroidAndroid Widget JavaUser code (UMC)PHPAjax

PositionOccurrence and UNICODE

<Initial string> and <Search string> can both correspond to:
  • ANSI strings.
  • 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 Managing UNICODE.
Reminder: The linguistic parameters used are defined during the call to ChangeCharset.
Component: wd290vm.dll
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 07/03/2023

Send a report | Local help