计算字符串中的字符串实例 [英] Count Instances Of String Within String

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

问题描述

是否有一行有效的代码来计算一个

字符串在另一个字符串中的实例数。


如果我有句子:

我想去公园,然后回家。


对于单词go,它会给我一个2的计数。


Derek

Is there an efficient line of code to count the number of instances of one
string within another.

If I have the sentence:
"I want to go to the park, and then go home."

It would give me a count of 2 for the word "go"

Derek

推荐答案

使用字符串比较方法


Dim s As String =我想去公园,然后回家。

MsgBox((String.Compare(s,go)+ 1).ToString &开始的行为)


问候


Michel Posseth [MCP]

" Derek哈特" <德******** @ yahoo.com> schreef in bericht

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...
use the string compare method

Dim s As String = "I want to go to the park, and then go home."
MsgBox((String.Compare(s, "go") + 1).ToString & " Occurances of go")

regards

Michel Posseth [MCP]
"Derek Hart" <de********@yahoo.com> schreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
是否有一个有效的代码行来计算一个
字符串在另一个字符串中的实例数。

如果我有句话:
我想去公园,然后回家。

它会给我一个2的数字go

Derek
Is there an efficient line of code to count the number of instances of one
string within another.

If I have the sentence:
"I want to go to the park, and then go home."

It would give me a count of 2 for the word "go"

Derek



我不相信有一种方法可以将它放在一行(至少不是在
VB.NET中,其他语言可能有一个内置的方法,这样做还是

可能会让你能够在一行上放置多个命令),但这里是一个简单的循环,可以在VB.NET中完成你想要的操作:

Dim teststring As String ="我想去公园,然后回家。

Dim i As Integer = -1

Dim stringcount As Integer = 0

而teststring.IndexOf(" go",i + 1)<> -1

stringcount + = 1

i = teststring.IndexOf(" go",i + 1)

结束时

您也可以将其编写为函数,如果您计划多次执行此操作,我强烈建议您这样做:

Public Function CountInstances(ByVal lookfor作为String,ByVal看作是

字符串)作为整数

Dim stringcount As Integer = 0

Dim i As Integer = -1

而lookin.IndexOf(lookfor,i + 1)<> -1

stringcount + = 1

i = lookin.IndexOf(lookfor,i + 1)

结束时

返回stringcount

结束函数

将它写为函数将需要几行代码来编写

函数,但之后你可以从一行调用它,使你的

代码更容易编写和调试:

stringcount = CountInstances(" go",teststring)

如果您有任何疑问,请随时提出。祝你好运!

-

Nathan Sokalski
nj ********@hotmail.com
http:/ /www.nathansokalski.com/

" Derek Hart" <德******** @ yahoo.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl ...
I don''t believe there is a way to put it in one line (at least not in
VB.NET, other languages might have a built-in method that does this or
possibly give you the ability to put multiple commands on one line), but
here is a simple loop that does what you want in VB.NET:
Dim teststring As String = "I want to go to the park, and then go home."
Dim i As Integer = -1
Dim stringcount As Integer = 0
While teststring.IndexOf("go", i + 1) <> -1
stringcount += 1
i = teststring.IndexOf("go", i + 1)
End While
You can also write it as a function, which I would strongly suggest if you
plan on doing this more than once:
Public Function CountInstances(ByVal lookfor As String, ByVal lookin As
String) As Integer
Dim stringcount As Integer = 0
Dim i As Integer = -1
While lookin.IndexOf(lookfor, i + 1) <> -1
stringcount += 1
i = lookin.IndexOf(lookfor, i + 1)
End While
Return stringcount
End Function
Writing it as a function will require the few lines of code to write the
function, but after that you can call it from just one line, making your
code simpler to write and debug:
stringcount = CountInstances("go", teststring)
If you have any questions, feel free to ask. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Derek Hart" <de********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
是否有一个有效的代码行来计算一个
字符串在另一个字符串中的实例数。

如果我有句话:
我想去公园,然后回家。

它会给我一个2的数字go

Derek
Is there an efficient line of code to count the number of instances of one
string within another.

If I have the sentence:
"I want to go to the park, and then go home."

It would give me a count of 2 for the word "go"

Derek



Derek,


您还可以使用正则表达式来匹配另一个字符串中的

字符串的实例。我*相信*我使用的模式返回

所有''go''的实例被空格包围,但是我再也不是很好

还有很好的RegEx 。

进口System.Text.RegularExpressions


模块主要

子主要()


Dim strtoSearch As String =我想去公园,然后

回家。

Console.WriteLine(GetStringOccurences(strtoSearch,

" go")。ToString)

Console.ReadLine()


结束子


私有函数GetStringOccurences(ByVal searchString,ByVal

searchWord)As Integer


Dim r As New Regex(String.Format(" \) s {0} \s",searchWord))

返回r.Matches(searchString).Count


结束函数

结束模块

Derek,

You could also use regular expressions to match the instances of a
string inside another string. I *believe* the pattern I use returns
all instances of ''go'' surrounded by spaces, but then again I''m not very
good with RegEx yet.
Imports System.Text.RegularExpressions

Module main
Sub main()

Dim strtoSearch As String = "I want to go to the park, and then
go home."
Console.WriteLine(GetStringOccurences(strtoSearch,
"go").ToString)
Console.ReadLine()

End Sub

Private Function GetStringOccurences(ByVal searchString, ByVal
searchWord) As Integer

Dim r As New Regex(String.Format("\s{0}\s", searchWord))

Return r.Matches(searchString).Count

End Function

End Module


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

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