2014年8月13日 星期三

在字串中找出 substring的位置

 /*
INDEX     (source,  excerpt  )  與條件完全相符的字串去找
FIND        (string,substring<,modifiers> <,startpos> )   與條件完全相符的字串去找,可設'I'忽略大小寫、'T'皆trim,搜尋的起點

INDEXC  (source,  excerpt-1 <,… excerpt-n> ) 以條件內的任一個字母、數字或符號去找
INDEXW (source, excerpt      <,delimiters>   )  以完全相符的單字組去找,預設分隔是space
*/


data test_index;
   a = 'ABC.DEF (X=Y)';
   b = 'X=Y';
  x_ab = index(a,b);     put x_ab= ;
  f_ab_1=find(a,b);      put f_ab_1=;
  f_ab_2=find(a,b,11);      put f_ab_2=; *超過了;
  f_ab_3=find(a,'a',  'I');      put f_ab_3=; *Ignore 大小寫;

length bt $10. ;
bt='X=Y';
  x_abt_1 = index(a, bt);     put x_abt_1= ;*bt尾要補至12位元的空白;
  x_abt_2 = index(a, trim(bt) );     put x_abt_2= ;
  F_abt_1 = find(a, bt, 'T' );     put f_abt_1= ; *兩者皆Trim;
 
c='X=Y=F' ;
  xc_ab_1 = indexc(a, c                       );     put xc_ab_1= ; *找到F;
  xc_ab_2 = indexc(a, c,  'abc.( )'       );     put xc_ab_2= ;

  wc_ab_1 = indexw(a, 'DEF'               );     put wc_ab_1= ;
  wc_ab_2 = indexw(a, 'DEF' ,    '.'      );     put wc_ab_2= ; *尾端有空白;
  wc_ab_3 = indexw(a, 'DEF' ,    '. '      );     put wc_ab_3= ;*逗點接空白;
run;

沒有留言: