楼主: jianlamhua
68117 30

[字符问题求助] 请教:如何删除字符串里的空格呢?   [推广有奖]

11
chenkaijun 在职认证  发表于 2013-9-22 18:18:29
如果proince下是各省的名称,而两个字名称的省份中间有空格,三个字名称的省份无空格,如何去掉两个字名称中间的空格呢?例如
province
北  京
天  津
黑龙江
内蒙古

12
chenkaijun 在职认证  发表于 2013-9-22 18:52:38
写出来了,是replace province=subinstr(province," ","",.),谢谢两位版主!

13
lich555 发表于 2013-11-10 17:25:45
真的太强了 非专业的实在不行  也是给wind和csmar逼的   这两个数据库就不能统一代码么

14
sunflower0209 发表于 2014-4-3 17:30:07
sungmoo 发表于 2009-4-21 18:02
replace x=subinstr(x," ","",.)*将x的所有观测值中的所有空格删除eblog
 金币 +5
 金钱& ...
我在merge数据时,有时上下两条数据中间就会空一行,请问这是什么原因?
如何将这样的空行去掉?还挺多,想通过程序删。。
谢谢!

15
轻舞飞杨 发表于 2014-4-7 14:37:30
sunflower0209 发表于 2014-4-3 17:30
我在merge数据时,有时上下两条数据中间就会空一行,请问这是什么原因?
如何将这样的空行去掉?还挺多, ...
sort排序 一下,之后drop in 1/n就行了,变量多就用一下foreach语句

16
玄一无相 在职认证  学生认证  发表于 2015-10-7 15:23:06
sungmoo 发表于 2009-4-21 18:02
replace x=subinstr(x," ","",.)*将x的所有观测值中的所有空格删除eblog
金币 +5
金钱& ...
变量前面有空格的话,好像用这个命令无效。。。到stata里面显示是个?,用此方法剔除问号也不行excel替换也不行,认不出那是空格

17
Lennydongsun 学生认证  发表于 2016-3-8 15:37:41
学习了,谢谢

18
pawdragon 在职认证  发表于 2016-12-13 18:34:03
但是我用replace x=subinstr(x," ","",.),  发现 有些变量后面的空格无法删除
于是 我用 replace x=trim(x)  还是不行,请温柔护额处理呢
@sungmoo

19
黃河泉 在职认证  发表于 2017-2-12 16:45:24
pawdragon 发表于 2016-12-13 18:34
但是我用replace x=subinstr(x," ","",.),  发现 有些变量后面的空格无法删除
于是 我用 replace x=trim(x ...
  1. clear
  2. input str16 x
  3. "hello    there "
  4. " hello    there"
  5. " hello    there "
  6. "hello there"
  7. end

  8. gen x0 = subinstr(x," ","",.)
  9. // 去除两边之空格
  10. gen x1 = trim(x)
  11. // 去除中间之空格
  12. gen x2 = stritrim(x1)

  13. list
复制代码
结果为
  1.     +--------------------------------------------------------------+
  2.      |                x           x0               x1            x2 |
  3.      |--------------------------------------------------------------|
  4.   1. |  hello    there    hellothere   hello    there   hello there |
  5.   2. |   hello    there   hellothere   hello    there   hello there |
  6.   3. |  hello    there    hellothere   hello    there   hello there |
  7.   4. |      hello there   hellothere      hello there   hello there |
  8.      +--------------------------------------------------------------+
复制代码
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
葫芦娃大王 + 1 + 1 精彩帖子
pawdragon + 5 + 2 + 2 + 2 精彩帖子

总评分: 经验 + 1  论坛币 + 6  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

20
zabbyy 发表于 2017-2-21 20:17:18
谢谢分享  学习了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-1 13:01