用于计算子字符串的字符串方法 [英] String method that counts substrings

查看:81
本文介绍了用于计算子字符串的字符串方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个字符串方法允许你给你的字符串中出现一个

子字符串的次数。


循环执行我的字符串IndexOf( substring,startPos),似乎

就像矫枉过正。


谢谢。

CR

解决方案

使用split方法!

如果字符串被拆分了2次,那么子字符串会出现2次!


i不知道这是不是最好的选择,但谁知道...


CodeRazor <钴******* @ discussions.microsoft.com>在消息中写道

news:48 ********************************** @ microsof t.com ...

是否有一个字符串方法允许你给你的字符串中出现一个
子字符串的次数。

循环我的字符串执行IndexOf(substring,startPos),似乎有点矫枉过正。

谢谢。
CR



是的,那就是我原来的想法,但似乎拆分方法

的工作原理是使用char将字符串拆分为子串。

我需要使用的分隔符必须比字符长。


例如string myString ="< p> hello< p> my nam is< p> ljksdf" ;;

我想将myString拆分成一个数组但是在每个< p>
之后
拆分方法只有在你使用一个字符的分隔符时才有效。


任何想法?


" CodeRazor" <钴******* @ discussions.microsoft.com> schrieb im Newsbeitrag

新闻:48 ********************************** @ microsof t.com ...

是否有一个字符串方法允许你给你的字符串中出现一个
子字符串的次数。

循环我的字符串执行IndexOf(substring,startPos),似乎有点矫枉过正。

谢谢。
CR



你好CR,


为什么你认为它有点矫枉过正。

没有实现它可以比搜索子串更简单

统计比赛。

你还在做什么呢?

实际上罗德里戈建议的方式更矫枉过正,因为它是'创建

额外的字符串实例(比计数多一个),从未使用过。


Christof


Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR

解决方案

Use the split method!
If the string was splited 2 times then the substring appears 2 times!

i don''t know if this is the best option but, who knows...

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...

Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR



yes, that''s what i thought originally, but it seems that the split method
works uses a char to split the string into substrings.
The delimeter i need to use has to be longer than a char.

e.g. string myString = "<p>hello<p>my nam is<p>ljksdf";
I want to split myString into an array but at after each <p>
The split method only works if you''re using a delimeter of one character.

Any ideas?


"CodeRazor" <Co*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:48**********************************@microsof t.com...

Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR



Hi CR,

why do you think it''s overkill.
No implementation could it do simpler than searching for the substring and
counting the matches.
What are you doing more?
In fact the way suggested by Rodrigo is more overkill, because it''s creating
additional string instances (one more than the count), wich never are used.

Christof


这篇关于用于计算子字符串的字符串方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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