急求:PB定位多个相同字符在字符串中的位置

hstc2010lzz 2015-04-15 03:46:09
如题:

我想实现的功能是这样的:
比如 ID name sex (空格代表tab pb代码中是“~t”)
定位tab的位置 返回 3 和 8

我知道有POS这个函数 只能获取到第一个值
有没有类似的函数。还是要用循环语句 来判断。。
...全文
881 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hstc2010lzz 2015-04-17
  • 打赏
  • 举报
回复
补充一下:读取到第n 项,用mid(string, st_locate.gi_location[n-1] -1 ,st_location.gi_charlen[n]) 当n = 2 ,mid(string, st_locate.gi_location[1] -1 ,st_location.gi_charlen[2]) = 567 应该是st_locate.gi_location[n-1] -1 ——》 st_locate.gi_location[n-1] + 1
hstc2010lzz 2015-04-17
  • 打赏
  • 举报
回复
我最主要想实现的效果是记录下字符串被特定字符分隔之后,每一子段的长度,和这个特定字符所在的位置 st_location.gi_charlen[]每一段字符的长度 st_locate.gi_location[] 分隔符在字符串中的位置 然后我可以很容易去读取一些有几十个子段,特定子段的内容 比如 可以从string = 123 567 89 1112 14 151617 19 读取到第n 项,用mid(string, st_locate.gi_location[n-1] -1 ,st_location.gi_charlen[n]) 当n = 2 ,mid(string, st_locate.gi_location[1] -1 ,st_location.gi_charlen[2]) = 567 用Pos ( string1, string2 {, start } )这种方法,也是可以定位出来,但是该定义的数组和结构还是得定义。谢谢lzp_lrp 解答
hstc2010lzz 2015-04-17
  • 打赏
  • 举报
回复
谢谢,还有这么一个参数设置啊!!!万分感谢
WorldMobile 2015-04-16
  • 打赏
  • 举报
回复
long ll_pos string ls_data = 'ID name sex' ll_pos = pos(ls_data, '~t', 1) do while ll_pos > 0 messagebox('', ll_pos) ll_pos = pos(ls_data, '~t', ll_pos + 1) loop 即可
WorldMobile 2015-04-16
  • 打赏
  • 举报
回复
晕,整这么复杂,用pos即可实现,它后面可以第三个参数 Description Finds one string within another string. Syntax Pos ( string1, string2 {, start } ) Argument Description string1 The string in which you want to find string2. string2 The string you want to find in string1. start (optional) A long indicating where the search will begin in string1. The default is 1. Return value Long. Returns a long whose value is the starting position of the first occurrence of string2 in string1 after the position specified in start. If string2 is not found in string1 or if start is not within string1, Pos returns 0. If any argument's value is null, Pos returns null. Usage The Pos function is case sensitive.
hstc2010lzz 2015-04-15
  • 打赏
  • 举报
回复
自己思考了一下午,终于解决了。。 贴上代码供大家参考一下,有什么好建议 可以提一下。 定义一个结构体 global type st_location from structure integer gi_location[] descriptor "comment" = "分隔符在字符串中的位置" integer gi_charlen[] descriptor "comment" = "每一段字符的长度" end type 定义一个 全局变量 st_location st_locate 定义一个全局函数 global type gf_locate from function_object //自定义的函数 end type forward prototypes global function st_location gf_locate (string as_string, string as_sign) end prototypes global function st_location gf_locate (string as_string, string as_sign);//as_string 待处理字符串 //as_sign 分隔符 //返回值: st_location.gi_charlen[]每一段字符的长度 // st_locate.gi_location[] 分隔符在字符串中的位置 long ll_count //分隔符的数目 long ll_pos string ls_right long ll_len string ls_temp ll_len = len(trim(as_string)) ls_temp = trim(as_string) if ll_len = 0 then return st_locate ll_pos = 1 do until ll_pos <= 0 ll_len = len(ls_temp) ll_count++ ll_pos = pos(ls_temp,as_sign) ls_right = right(ls_temp,ll_len - ll_pos) if ll_count = 1 then st_locate.gi_charlen[ll_count] = ll_pos - 1 st_locate.gi_location[ll_count] = ll_pos else st_locate.gi_charlen[ll_count ] = ll_pos - 1 st_locate.gi_location[ll_count] = ll_pos +st_locate.gi_location[ll_count - 1] end if ls_temp = ls_right ll_pos = pos(ls_temp,as_sign) loop st_locate.gi_charlen[ll_count+1] = len(ls_temp) return st_locate end function 调用代码 long ll_count string ls_message integer li_fileread integer li_filehandle string ls_string string ls_ID string ls_name string ls_sex li_filehandle = fileopen("789.txt",linemode!) li_fileread = fileread(li_filehandle,ls_string) st_locate = gf_locate(ls_string,"~t") ls_ID = left(ls_string,st_locate.gi_location[1] - 1) ls_name = mid(ls_string,st_locate.gi_location[1]+1,st_locate.gi_charlen[2]) ls_sex = right(ls_string,len(ls_string) - st_locate.gi_location[2]) sle_1.text = ls_ID sle_2.text = ls_name sle_3.text = ls_sex

740

社区成员

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

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