strindx¶
Purpose¶
Finds the index of one string within another string.
Format¶
-
idx =
strindx
(haystack, needle[, start])¶ - Parameters:
haystack (string or scalar) – the data to be searched.
needle (string or scalar) – the substring to be searched for in haystack.
start (scalar) – Optional argument, the starting point of the search in haystack for an occurrence of needle. The index of the first character in a string is 1. Default=1.
- Returns:
idx (scalar) – contains the index of the first occurrence of needle, within haystack, which is greater than or equal to start. If no occurrence is found, it will be 0.
Examples¶
Dataframe example¶
// Create file name with full path
fname = getGAUSSHome() $+ "examples/auto2.dta";
// Load 'rep78' variable
rep78 = loadd(fname, "rep78");
// Print out the first 5 observations
head(rep78);
The above code will print out:
rep78
Average
Average
.
Average
Good
Now we will find the index of the start of “age”.
// Find the index of "age" in 'rep78'
idx = strindx(rep78, "age");
// Print the first 5 observations of 'idx'
head(idx);
5.0000000
5.0000000
0.0000000
5.0000000
0.0000000
String array example¶
// Create a 3x1 string array
state = "alaska" $|
"alabama" $|
"arkansas";
// Find the first instance of the
// letter 'a' starting from
// the front of the string
strrindx(state, "a");
Since the search starts from the first character, the above code will print out:
1.0000000
1.0000000
1.0000000
// Find the first instance of the
// letter 'a' starting from the
// 5th character of the string
strindx(state, "a", 5);
This time, the search will start from the 5th character and continue searching towards the last character, resulting in:
6.0000000
5.0000000
7.0000000
String example¶
An example of the use of this function is the location of a name within a string of names:
// String to search in
haystack = "nameagepaysex";
// String to search for
needle = "pay";
// Find starting index of 'needle'
idx = strindx(haystack, needle, 1);
The above code will set idx equal to:
8.00
This function is used with strsect()
for extracting substrings.
See also
Functions strrindx()
, strlen()
, strsect()
, strput()
, strreplace()