ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / String functions
  • Finding a character string
  • Unicode/Ansi
  • Characters taken into account for punctuation and spaces
  • Miscellaneous
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 position of a specified string within another string (or Buffer).
You can also find the position of one of the strings in an array.
Example
n is int
MyString is string = "What a wonderful World"
n = Position(MyString, "won", 1, FromBeginning)
// Returns 8 (position of "w" in "wonderful")
n = Position(MyString, "XXX")
// Returns 0 (no "XXX" found in the string)
n = Position(MyString, "l", 0, FromEnd)
// Returns 21 (position of "l" in "World")
n = Position(MyString, "e", 11)
// Returns 12 (position of "e" in "wonderful")
n = Position(MyString, "o", Length(MyString), FromEnd)
// Returns 19 (position of "o" in "World")
n = Position(MyString, "o", n-1, FromEnd)
// Returns 9 (position of "o" in "wonderful")
n = Position(MyString, "o", n-1, FromEnd)
// Returns 0 (there is no other "o")
n is int
MyString2 is string = "http://Server/File.html"
n = Position(MyString2,["/","//"])
// Returns 6
n = Position(MyString2,["/","//"], 8)
// Returns 15
Syntax

Finding the position of a specified string within another string Hide the details

<Result> = Position(<Initial string> , <Search string> [, <Start position> [, <Option>]])
<Result>: Integer
  • Position of the first character of the search string within the initial string,
  • 0 if the search string is not found.
    This position is relative to the beginning of the string.
<Initial string>: Character string
Character string where the search will be performed (maximum size: 2 GB).
<Search string>: Character string
String to be searched in the initial string.
<Start position>: Optional integer
Index of the character from which the search will be started. By default, the search starts from the first character (character 1).
To start the search from the end of the string (FromEnd constant), this parameter must correspond to 0 or to the length of the string.
If this parameter is negative, the search starts from the first character.
If the value of this parameter is greater than the number of characters in <Initial string>, <Result> is equal to 0.
<Option>: Optional constant (or combination of constants)
Indicates the search direction and the characteristics of the search:
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).
Linux This option has no effect.
WholeWordSearches for a whole word, i.e., between punctuation characters or spaces.
Linux This option has no effect.
WEBDEV - Browser code This parameter is not available. The search is performed from the first to the last character of the string. The search is case sensitive. The search string does not necessarily correspond to a whole word: this string can be part of a word.

Finding a string in an array of strings Hide the details

(<Result>, <Index>) = Position(<Initial string> , <Array of search strings> [, <Start position> [, <Option>]])
<Result>: Integer
  • Position of the first character of the first string of the array found in the initial string.
    This position is relative to the beginning of the string.
  • 0 if none of the search strings were found.
<Index>: Integer
Index of the string in the array.
<Initial string>: Character string
Character string where the search will be performed (maximum size: 2 GB).
<Array of search strings>: Array
Array containing the strings to find in the initial string.
<Start position>: Optional integer
Index of the character from which the search will be started. By default, the search starts from the first character (character 1). To start the search from the end of the string, this parameter must correspond to 0 or to the length of the string.
If this parameter is negative, the search starts from the first character.
If the value of this parameter is greater than the number of characters in <Initial string>, <Result> is equal to 0.
<Option>: Optional constant (or combination of constants)
Indicates the search direction and the characteristics of the search:
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).
Linux This option has no effect.
WholeWordSearches for a whole word, i.e., between punctuation characters or spaces.
Linux This option has no effect.
WEBDEV - Browser code This parameter is not available. The search is performed from the first to the last character of the string. The search is case sensitive. The search string does not necessarily correspond to a whole word: this string can be part of a word.
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.
The positions in a character string are always given from the beginning of the string, regardless the search direction.
To associate Position with the FromEnd constant, it is necessary to start at the position calculated by Length(<Initial string>). For example:
Position(MyString, "\", Length(MyString), FromEnd)
The following syntax can also be used. In this case, the parameter 0 allows the function to select the best start position.
Position(MyString, "\", 0, FromEnd)

Unicode/Ansi

Position allows you to find the position of a string in a Unicode or Ansi string.
Ansi and/or Unicode strings can be used in <Initial string> and <Search string> (you can use a Unicode parameter and an Ansi parameter).
The conversion rule used is as follows:
  • If at least one of the strings is a Unicode string, all strings are converted to Unicode and the operation is performed in this standard.
  • Otherwise, the operation is performed in Ansi.

Characters taken into account for punctuation and spaces

The characters taken into account for punctuation and spaces are provided by the system. To get the list of these characters, write the following WLanguage code:
s is string
FOR i = 0 TO 255
IF Charact(i) <> StringFormat(Charact(i), ccIgnorePunctuationAndSpace) THEN
 s += Charact(i)
END
END
Info(s)
ToClipboard(s)

Miscellaneous

PositionOccurrence is used to find the position of the Nth occurrence of a substring by searching from the beginning or from the end of the string.
Component: wd290vm.dll
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/21/2022

Send a report | Local help