Hi 你好,欢迎访问!登录 文章归档 免责声明 文章投稿
当前位置:首页 - 未命名 - 正文 长期分享互联网网赚项目!

Linux编程:substr函数用法详解

2017-12-17未命名网络转载658°c
A+ A-

substr(string, start<,length>):从string的start位置开始提取字符串

length:要提取字符串的长度,若length为以下任意条件之一时,返回start位置到串尾的所有字符:
length不指定 
length为空 
length为负数
length大于start到串尾的长度

例如:

data test; 
str='chsh234960b3'; 
strl=subatr(str,5); 
str2=substr(str,5,'');
str3=aubstr(str,5,-3);
str4=aubstr(str,5, 30);drop atr;
run;proc print noobs; 
run;

大数据Linux学习群:284059605点击链接加入群【大数据Linux学习】:大数据Linux学习

注意两点:
substr中的start为负数时返回空值;
当length为负数时,默认从start位罝取所有字符。

substr实现从右侧取数几种方法:从字符串chah234960b3右侧截取5个字符的几种方法

1.substr(str,length(str)-n+1,n)
data _null_;str='chah234960b3';
str1=substr(str,length(str)-5+1,5);
put str1=;
run;2.使用reverse函数
data _null_;str='chah234960b3';
str1=reverse(substr(reverse(str),1,5));
put str1=;
run;

substr除了有字段截取的功能外,还可以用来替换字段。

'='在左边: 截取字段
data _null_;
x='123456';str=substr(x,1,3);
put str=;
run;
在log中输出结果为: str=123;'='在右边: 替换字段
data _null_;
x='123456';
substr(x,1,3)='789';
put x=;
run;
在log中输出结果为: str=789456;

通常情况下,substr往往结合find一起使用。

例1: 截取每行数据中的math和english
data test;                                                                                                                                     
input id :$3. exams & $30.;                                                                                                                    
math=substr(exams,find(exams,'math','i',1),4);                                                                                                 
english=substr(exams,find(exams,'english','i',1),7);                                                                                           
cards;                                                                                                                                         
001 math Chinese                                                                                                                               
002 english math chemistry                                                                                                                     
003 Chinese phisics                                                                                                                            
;                                                                                                                                              
run;                                                                                                                                                                                                                                                                                         
                                                                                                                                               
例2: 查找每位学生的数学和英语成功                                                                                                              
data test;                                                                                                                                     
input id $ exams & $30.;                                                                                                                       
math=substr(exams,find(exams,'math','i',1)+5,3);                                                                                               
english=substr(exams,find(exams,'english','i',1)+7,3);                                                                                         
cards;                                                                                                                                         
001 math 100 english 90                                                                                                                        002 math 80 english 70                                                                                                                         003 math 95 english 85                                                                                                                         ;                                                                                                                                              
run;                                                                                                                                           
                                                                                                                                               
data test;                                                                                                                                     
input id $ exams & $30.;                                                                                                                       
math=substr(exams,index(exams,'math')+5,3);                                                                                                    
english=substr(exams,index(exams,'english')+7,3);                                                                                              
cards;                                                                                                                                         
001 math 100 english 90                                                                                                                        002 math 80 english 70                                                                                                                         003 math 95 english 85                                                                                                                         ;                                                                                                                                              
run;


  选择分享方式
  移步手机端
Linux编程:substr函数用法详解

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签:
推荐阅读

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>


  用户登录