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   <[email protected]>
Subject   st: RE: string function
Date   Mon, 13 Oct 2003 17:58:27 +0100

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. 


index(string, ltrim(string)) 

is another way to step towards what you 

I don't know a way to specify "not a space" 

[email protected] 
*   For searches and help try:

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