IDL 字符串操作
1 字符串拼接
IDL> arr=['My name', 'is', 'QQ']
IDL> print, strjoin(arr) ;直接拼接字符串
My nameisQQ
IDL> print, strjoin(arr,'\t') ;用'\'对字符串进行拼接
My name\tis\tQQ
IDL> print, strjoin(arr,' ') ;用' '空格拼接字符串
My name is QQ
2 字符串长度
IDL> print, strlen(arr[0])
7
IDL> print, strlen(arr)
7 2 2
3 大小写转换
IDL> print, strlowcase('My name')
my name
IDL> print, strupcase('My name')
MY NAME
4 移除空格
IDL> print, strcompress(' My name ', /remove_all) ;移除所有空格
Myname
IDL> str = strtrim(' My name is QQ ', 0) ;移除尾部空格
IDL> help, str
STR STRING = ' My name is QQ'
IDL> str = strtrim(' My name is QQ ', 1) ;移除首部空格
IDL> help, str
STR STRING = 'My name is QQ '
IDL> str = strtrim(' My name is QQ ', 2) ;移除两端空格
IDL> help, str
STR STRING = 'My name is QQ'
5 字符比较
IDL> a='My name is QQ'
IDL> b='My name Is WebChat'
IDL> help, strcmp(a,b) ;整个字符串比较
<Expression> BYTE = 0 ; 不等
IDL> help, strcmp(a,b,8) ;前8个字符进行比较('My name ')
<Expression> BYTE = 1 ;相等
IDL> help, strcmp(a,b,9) ; 前9个字符比较
<Expression> BYTE = 0 ;不等
IDL> help, strcmp(a,b,9,/fold_case) ;前9个字符比较(不区分大小写)
<Expression> BYTE = 1 ;相等
6 字符串查找
IDL> help,strpos('My name is QQ','QL') ;从头开始找首次出现的字符索引
<Expression> LONG = -1 ;没有找到
IDL> help,strpos('My name is QQ','QQ')
<Expression> LONG = 11 ;找到索引
IDL> help,strpos('My name is QQ','QQ',/reverse_search) ;虽然设置了reverse_search, 依旧从头开始找首次出现的字符索引
<Expression> LONG = 11
IDL> help,strpos('My name is QQ','is', 7, /reverse_search) ;从索引为7的位置开始,反向寻找'is'
<Expression> LONG = -1 ;没有找到
IDL> help,strpos('My name is QQ','is', 8, /reverse_search) ;从索引为8的位置开始,反向寻找'is'
<Expression> LONG = 8 ;找到索引
IDL> help,strpos('My name is QQ','is', 8) ;从索引为8的位置开始,正向寻找'is'
<Expression> LONG = 8 ;找到索引
7 获取字符串
help, strmid('My name is QQ',8, 2) ;从索引8的位置, 获取两个字符
<Expression> STRING = 'is'
8 字符拆分
IDL> print, strsplit('My name is QQ',' ') ;基于原有字符中的空格拆分字符串
0 3 8 11 ;返回拆分后字符串在原字符串的位置索引
IDL> arr=strsplit('My name is QQ',' ',/extract) ;返回拆分后的字符串
IDL> help, arr
ARR STRING = Array[4]
IDL> print, arr
My name is QQ
IDL> print, strsplit('My name is QQ',' ',/extract, count=count, length=length) ;返回拆分后字符串数量
My name is QQ
IDL> print, count ;返回拆分后字符串总数
4
IDL> print, length ;返回拆分后每个字符串的长度
2 4 2 2
9 字符匹配
IDL> str = ['foot', 'Feet', 'fate', 'FAST', 'ferret', 'fort']
IDL> PRINT, str[WHERE(STRMATCH(str, 'f??t', /FOLD_CASE) EQ 1)] ;忽略大小写, 匹配首尾字符为'f'与't', 中间有2个字符的字符串
foot Feet FAST fort
PRINT, str[WHERE(STRMATCH(str, 'f*t', /FOLD_CASE) EQ 1)] ;忽略大小写, 匹配首尾字符为'f'与't', 中间字符不限的字符串
foot Feet FAST ferret fort