一个困惑很久的格式显示问题

lcy5415 2003-12-11 09:36:43
表中有个字段如;mytext

里面录入的内容为:1_3_4_6_9_23
通过读数据库得到 string tem="1_3_4_6_9_23";后,我怎么能拆分出所要的值

1
3
4
6
9
23

...全文
40 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
azsoft 2003-12-14
  • 打赏
  • 举报
回复
自己解决了
laodeng 2003-12-14
  • 打赏
  • 举报
回复
不好意思噢,昨晚写的程序有小小的问题,如果是要实现你的那种折分的话,具体的程序应该是这样的:

   using System.Text.RegularExpressions;

string[] strArray;
string tem="1_2_3_4";
Regex rex=new Regex("{_}");//注意,这里使用了一个使用了一个实例对像,本来
//不想用的,但是不知为什么真接用它的类方法不行呢。只好用这个了,"{_}"表示的是要
//匹配的正则表达式模式。
strArray=rex.Split(tem);//这个参数是要进行匹配的正则表达式模式进行折分的字
//符串,这样strArray中就会存有"1","_","2","_","3","_","4"这些数据。之后你想怎么取
//都可以了。


学艺不精,请大家多多指教!
laodeng 2003-12-14
  • 打赏
  • 举报
回复
不好意思噢,昨晚写的程序有小小的问题,如果
zjcxc 元老 2003-12-14
  • 打赏
  • 举报
回复
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_split]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_split]
GO

--用个函数就行了.
create function f_split(@str varchar(8000))
returns @re table(id int identity(1,1),re varchar(1000))
as
begin
declare @tb table(id int identity(1,1),a int)
insert into @tb(a) select top 8000 null from
(select id from syscolumns) as a,(select id from syscolumns) as b
insert into @re(re)
select substring(name,b.id,charindex('_',name+'_',b.id)-b.id)
from (select name=@str) a join @tb b on substring('_'+name,b.id,1)='_'
return
end
go

--调用示例:
select * from dbo.f_split('1_3_4_6_9_23')

/*--结果
id re
----------- ------
1 1
2 3
3 4
4 6
5 9
6 23

(所影响的行数为 6 行)

--*/
laodeng 2003-12-11
  • 打赏
  • 举报
回复
这个你可以用Regex类来实现呀,它有一个Split()方法,就是实现这个功能的哦!

相关的程序代码如下:

using System.Text.RegularExpressions;

string[] strArrary;//用于存放折分之后的字符串
string tem="1_3_4_6_9_23";//这个就是你取出来的字符串
strArrary=Regex.Split(tem,'_');//tem就是一个要折分的字符串,_ 就是折分分隔符

这样就可以实现折分的功能了。
我是这么写的,不知对你有没有用。

happydreamer 2003-12-11
  • 打赏
  • 举报
回复
转大力的


create function getstrofindex (@str varchar(8000),@index int =0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next =1
select @location = charindex(',',@str,@start)
while (@location <>0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(',',@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1
select @str_return = substring(@str,@start,@location -@start)
if (@index <> @next ) select @str_return = ''
return @str_return
end
go
select dbo.getstrofindex('aa vv,cc ee, sdfasdfasf',3)
happydreamer 2003-12-11
  • 打赏
  • 举报
回复
declare @string varchar(1000)
declare @xh char(10)
declare @sh varchar(13)
set @string='1_3_4_6_9_23'
set @string='select * into ##tmp from (select '''+replace(@string,'_','''as str union all select ''')+''') a'
exec(@string)
print @string

select * from ##tmp
drop table ##tmp
CrazyFor 2003-12-11
  • 打赏
  • 举报
回复
参考:

--N要连继,
select top 8000 identity(int,1,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c

---------------------------------------------------------------------
declare @a table (id int,string varchar(8000))
insert @a select 1 ,'a,b,c,sd,dfsdfg'
union select 2, 'a,n,sdf,we,t'
union select 3, 's,df,df'



select a.*,b.*,id,substring(','+string+',',N+1,charindex(',',','+string+',',N+1)-(N+1))
from @a a,numtab b
where substring(','+string+',',N,8000) like ',_%'
order by id,N

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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