Ali
Ali

Reputation: 1

How to remove a long space from a string

I have a string variable in my dataset with a large space and I am not sure how i can remove it.

I have already tried:

replace x = subinstr(x," ", "", .)

However, nothing happened.

Below you can find an example of the string:

Used: An item that has been used previously. The item may have some signs of cosmetic wear, but is fully
operational and functions as intended. This item may be a floor model or store return that has been used.

I am trying to convert it to:

Used: An item that has been used previously. The item may have some signs of cosmetic wear, but is fully operational and functions as intended. This item may be a floor model or store return that has been used. See the seller's listing for full details and description of any imperfections.

Upvotes: 0

Views: 4494

Answers (2)

user9367317
user9367317

Reputation:

Rather than counting the number of space characters, just trim the consecutive internal space characters with stritrim(). So,

replace x = stritrim(x)

If you are still running into issues, there may be other hidden characters lurking in the string. In which case you may want to use something like Nick Cox's charlist.

Upvotes: 1

user8682794
user8682794

Reputation:

You basically have to tell Stata to eliminate all spaces that are two characters long or greater.

The following works for me:

clear
set obs 1

generate string = "Used: An item that has been used previously. The item may have some signs of cosmetic wear, but is fully" ///
+ "                                                                       operational and functions as intended. This item may be a floor model or store return that has been used."
generate new_string = subinstr(string,"  ", "", .)

list

Note that i changed " " to " ".

Upvotes: 1

Related Questions