循环列出字符串中每个数字的出现时创建问题 [英] Problem creating while Loop listing each occurance of a number in a string

查看:57
本文介绍了循环列出字符串中每个数字的出现时创建问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我创建了一个while循环,它基本上会遍历字符串中的每个数字并列出所需数字的位置。


例如假设我有一个字符串sa" 12123124"我想列出1的所有位置。输出应为0,2和5.


我的工作如下:

Hi,

I created a while-loop which will basically go through each number in a string and list the positions of the desired number.

For example suppose I have a string sa "12123124" and I want to list all the positions of "1" the output should be 0, 2 and 5.

My work out below is:

展开 | 选择 | 换行 | 行号

推荐答案

这是最后一个周期。在增量
It''s the last cycle. In the incrementation


posFirst(在那一刻:false)设置为1(假+1 = 1),你可以突破(if(is_boolean(
posFirst (which is at that moment: false) is set to 1 (false +1 = 1), you could either break out (if (is_boolean(


posFirst))break;)或使用带有PREG_OFFSET_CAPTURE标志的preg_match_all()而不是while循环。


问候
posFirst)) break;) or use preg_match_all() with the PREG_OFFSET_CAPTURE flag instead of the while loop.

regards


这篇关于循环列出字符串中每个数字的出现时创建问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆