字符串实例计数 [英] String instance count

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

问题描述

我想在另一个字符串中计算某个字符串(分隔符)的实例数。我没有看到在框架中执行此操作的功能(如果

有,请指向我)。如果没有,有人可以告诉我,如果我以下使用的

方法是有效的,或者是否有更好的方法可以做到这一点,因为

这些将是我正在搜索相当大的字符串。谢谢


公共共享函数CountDelimiter(ByVal strInput As String,ByVal

strDelimiter As String)As Int32

Dim iStart为Int32,iCount为Int32,iResult为Int32


iStart = 1

iCount = 0




''iResult成为找到分隔符的位置。如果为0,则不是找到


iResult = InStr(iStart,strInput,strDelimiter)

如果iResult = 0则退出Do

''每增加一次我们的计数var

iCount + = 1

''将我们的下一个开始位置增加为下一个字符后

当前找到的位置

iStart = iResult + 1

循环


返回iCount

结束功能


-

--------

Jon Rosenberg

解决方案

Jon Jon,

我总是在寻找它,但是当我看到你的代码时我想到了/>
这个愚蠢的

\\\\\\\

Dim a As String =" abaaa abbba babaabaabaab"

Dim b As String()= Split(a," ab")

MessageBox.Show((b.Length - 1).ToString)

///////

当你没有得到更好的答案时你可以尝试一下,做g一些小测试

它确实有效。

当然是愚蠢的。

Cor

" COR'QUOT; < no*@non.com> scripsit:

我总是在寻找那个,但是当我看到你的代码时我觉得这个很愚蠢
\\\\\\ \\ /
Dim a As String =" abaaa abbba babaabaabaab"
Dim b As String()= Split(a," ab")
MessageBox.Show((b.Length - 1).ToString)
///////
当你没有得到更好的答案时你可以尝试一下,做一些小测试
它确实有效。
当然是愚蠢的。




是的,它有效。但是我认为如果你拆分一个10 b / b
MB的字符串,这不是最好的解决方案。


;-)


-

Herfried K. Wagner

MVP·VB Classic,VB.NET

< http:// www。 mvps.org/dotnet>


Herfried,

其他样本确实可以看到每个增量1个增量的循环>每次
字符。

我不太确定这是否更快,

但是好的让我们做一笔交易,你输入一个10Mb的字符串,而不是我将制作

测试程序。

Cor


I want to count the number of instances of a certain string(delimiter) in
another string. I didn''t see a function to do this in the framework (if
there is, please point me to it). If not, could someone let me know if the
method I''ve used below is efficient or if there is a better way to do it, as
these will be rather large strings I''m searching in. Thanks

Public Shared Function CountDelimiter(ByVal strInput As String, ByVal
strDelimiter As String) As Int32
Dim iStart As Int32, iCount As Int32, iResult As Int32

''Set our vars to base values
iStart = 1
iCount = 0

Do
''iResult becomes the position where delimiter is found. If 0, not
found.
iResult = InStr(iStart, strInput, strDelimiter)
If iResult = 0 Then Exit Do
''Increment our count var for each time it is found
iCount += 1
''Increment our next start position to be the next char after the
currently found position
iStart = iResult + 1
Loop

Return iCount
End Function

--
--------
Jon Rosenberg

解决方案

Hi Jon,
I am too always looking for that, but when I did look at your code I thought
this stupid
\\\\\\\
Dim a As String = "abaaa abbba babaabaabaab"
Dim b As String() = Split(a, "ab")
MessageBox.Show((b.Length - 1).ToString)
///////
When you don''t get a better answer you can try it, doing some little tests
it did work.
It is stupid of course.
Cor


"Cor" <no*@non.com> scripsit:

I am too always looking for that, but when I did look at your code I thought
this stupid
\\\\\\\
Dim a As String = "abaaa abbba babaabaabaab"
Dim b As String() = Split(a, "ab")
MessageBox.Show((b.Length - 1).ToString)
///////
When you don''t get a better answer you can try it, doing some little tests
it did work.
It is stupid of course.



Yes, it works. But I think it''s not the best solution if you split a 10
MB string.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>


Herfried,
The other samples does as far as I can see a loop from 1 increment per
character each time.
I am not so sure if that is faster,
But ok lets make a deal, do you type a 10Mb string, than will I make the
testprogram.
Cor


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

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