如何从字符串里提取指定的字符

xxzsx 2010-01-28 11:31:27
各位,有个字符串0.6*1219*1287/0.6*1219*1433/0.6*1219*1615/0.6*1219*1400/0.6*1219*1043,我想提取每个/前的子字符串,比如0.6*1219*1287,并且在每个子字符串里提取*前后的字符,如0.6,1219,1287,怎么实现啊
...全文
137 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sewinten 2010-01-28
  • 打赏
  • 举报
回复
string ls_str = '0.6*1219*1287/0.6*1219*1433/0.6*1219*1615/0.6*1219*1400/0.6*1219*1043'
string ls_arr[]
long ll_pos, i = 0 , ll_start

ls_str += "*"
ll_pos = pos(ls_str,'*')
ll_start = 1
do while ll_pos > 0 and ll_pos <= len(ls_str)
i++
ls_arr[i] = mid(ls_str,ll_start,ll_pos - ll_start)
ll_start = ll_pos + 1
ll_pos = pos(ls_str,'*',ll_start)
loop

ls_arr[]就是你要提取的字符串
zb63668331 2010-01-28
  • 打赏
  • 举报
回复

--给你数据库中拆分表方法

--> --> (Roy)生成測試數據

if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[COl2] nvarchar(5))
Insert Tab
select 1,N'a,b,c' union all
select 2,N'd,e' union all
select 3,N'f'
Go

SQL2000用辅助表:
if object_id('Tempdb..#Num') is not null
drop table #Num
go
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID)
from
Tab a,#Num b
where
charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','


SQL2005用Xml:

select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,''+replace(COl2,',','')+'') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b




SQL05用CTE:

;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)

生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/


TheLittlePrince 2010-01-28
  • 打赏
  • 举报
回复
呵呵,同意楼上的,用pos和mid也行,写个循环慢慢执行吧。呵呵
archlwh 2010-01-28
  • 打赏
  • 举报
回复
pos,left函数结合用塞
xxzsx 2010-01-28
  • 打赏
  • 举报
回复
晕,楼主,人家楼上写的这么详细了,你稍微改动下就可以了啊,你把那个函数里面的*替换成/,再照上面执行一下代码塞,,,你说的很容易啊
wag_enu 2010-01-28
  • 打赏
  • 举报
回复

public function long uf_parsetoarray (string as_source, string as_delimiter, ref string as_array[]);long ll_dellen
long ll_pos
long ll_count
long ll_start
long ll_length
string ls_holder
long ll_null

if ((isnull(as_source)) or (isnull(as_delimiter))) then
setnull(ll_null)
return ll_null
end if

if trim(as_source) = "" then
return 0
end if

ll_dellen = len(as_delimiter)
ll_pos = pos(upper(as_source),upper(as_delimiter))

if ll_pos = 0 then
as_array[1] = as_source
return 1
end if

ll_count = 0
ll_start = 1

do while ll_pos > 0
ll_length = ll_pos - ll_start
ls_holder = mid(as_source,ll_start,ll_length)
ll_count ++
as_array[ll_count] = ls_holder
ll_start = ll_pos + ll_dellen
ll_pos = pos(upper(as_source),upper(as_delimiter),ll_start)

loop

ls_holder = mid(as_source,ll_start,len(as_source))

if len(ls_holder) > 0 then
ll_count ++
as_array[ll_count] = ls_holder
end if

return ll_count
end function


调用像这样:


long li_ocount
string ls_tmp
string ls_o[]

ls_tmp = "0.6*1219*1287/0.6*1219*1433/0.6*1219*1615/0.6*1219*1400/0.6*1219*1043"

li_ocount = uf_parsetoarray(ls_tmp,"/",ls_o)
int i
for i = 1 to li_ocount
string ls_ot[]
int li_ccount
li_ccount = uf_parsetoarray(ls_o[i],"*",ls_ot) //这时,ls_ot[] 即是所要的数据.
next



archlwh 2010-01-28
  • 打赏
  • 举报
回复
晕,楼主,人家楼上写的这么详细了,你稍微改动下就可以了啊,你把那个函数里面的*替换成/,再照上面执行一下代码塞
xxzsx 2010-01-28
  • 打赏
  • 举报
回复
谢谢楼上,但提取出来的里面没有把/也给分开,出现1287/0.6

740

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 脚本语言
社区管理员
  • 脚本语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧