Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: string function


From   "Nick Cox" <[email protected]>
To   "Nick Cox" <[email protected]>, <[email protected]>
Subject   st: RE: string function
Date   Mon, 13 Oct 2003 18:03:45 +0100

Nick Cox 
> 
> Rio, Martin
>  
> > I would like to specify a command to tell me the position 
> > in a string of the
> > first character that is not a space. Example:
> > 
> > " one" would yield 1
> > "  two" would yield 2
> > "   three" would yield 3
> > and so on...
> > 
> > Just so you know why in the world I'd want to do this, the 
> > number of spaces
> > determines hierarchy in my dataset. I was thinking of using 
> > the index
> > function, but I'm not sure how to specify 'not a space' for 
> > the second
> > argument.
> 
> By my count the "o" in " one" is at position 2, 
> etc. using the Stata convention which is to start at 1. 
> 
> It seems however that you may want to start 
> counting at 0. As the difference is obvious, 
> I will just look at start 1. 
> 
> One way to do this to get the number of leading 
> spaces from 
> 
> length(string) - length(ltrim(string)) 
> 
> and to add 1. 
> 
> Alternatively, 
> 
> index(string, ltrim(string)) 
> 
> is another way to step towards what you 
> want. 
> 
> I don't know a way to specify "not a space" 
> directly. 

I do, actually: it's 

!= " " 

but I don't think that helps here, without 
putting it a loop, which we wish to avoid 
mightily in problems like these.  

Nick 
[email protected] 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index