Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Wishlist for Stata 13 - index()

From   Alan Riley <>
Subject   Re: st: Wishlist for Stata 13 - index()
Date   Thu, 6 Jun 2013 00:09:55 -0500

Chuntao ( is concerned that Stata may drop the
-index()- function.  He also finds the -indexnot()- function to be
a bit confusing:

> The string function index(s1,s2) is quite useful and still usable in
> Stata12, which returns the position of the first character of s2 if it
> can be found in s1, and zero otherwise. Unfortunately, this function
> is no longer listed in manual and her helps are also missing. New
> users have to use indexnot(), which is not straight forwardly defined
> and many user may find it quite strang.
> Following is the output of some indexnot() and i actually totally lost
> with them;
> . di indexnot("d", "abcdefg")
> 0
> . di indexnot("abcdefg", "a")
> 2
> . di indexnot("abcdefg", "c")
> 1
> . di indexnot("abcdefg", "d")
> 1
> . di indexnot("abcdefg", "s")
> 1
> . di indexnot("d", "abcdefg")
> 0
> . di indexnot("a", "abcdefg")
> 0
> I suggest Stata 13 to keep the index() function, if do not want to
> drop indexnot()

First, let me assure Chuntao that the functionality provided by
-index()- will not go away.  In Stata 9, the -index()- function
was renamed to -strpos()-.  So, to find help for it now, Chuntao
can type -help strpos()-.  Both -index()- and -strpos()- exist
in Stata 12 and do the same thing.

By the way, now is a good time to point out that if you know the name of
a Stata function (as opposed to a Stata command), the proper way to view
the help file for it is to include the parentheses after the function
name when you type -help-:

   . help index()

   . help strpos()

Because Chuntao expressed some confusion about the -indexnot()-
function, I will also try to explain how it works.  It takes
two arguments.  The first -- let's call it "s1" -- is a string
to be searched.  The second -- let's call it "s2" is a set of
individual characters to search for within s1.  -indexnot()-
returns the index within s1 of the first character NOT in s2.

Here are a few examples that Chuntao tried, along with an explanation
for each:

   . display indexnot("d", "abcdefg")

     s1 is "d".  s2 is the set of characters "abcdefg".  Notice that
     "d" is one of the characters in s2.  Given that, what is the position
     of the first character in s1 not found in s2?  Well, there isn't
     one, because "d" is in s2, and "d" is found in s1.  So, -indexnot()
     returns 0.

   . display indexnot("abcdefg", "a")

     s1 is "abcdefg" and s2 contains only 1 character, "a".  The first
     position within s1 where a character from s2 is not found is "2",
     which is what indexnot() returns.  "a" is found as the first character
     in s1, but the second character in s1, "b", is not among the
     characters listed in s2, so "2" is the value returned.

   . display indexnot("abcdefg", "c")

     s1 is again "abcdefg", and s2 is the single character "c".  The first
     character in s1 that is not also in s2 is the very first character of
     s1, "a".  So, the value returned by -indexnot()- is 1.

I hope this makes -indexnot()- a little more clear.

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index