开发者

how to find the number sentences in a string

开发者 https://www.devze.com 2023-02-09 20:58 出处:网络
How to find the number for sentences in a String data ? Edit: Ok I came up with this after following Wipqozn\'s method

How to find the number for sentences in a String data ?

Edit: Ok I came up with this after following Wipqozn's method

Dim str As String = "This . is .  a . text."
Dim maxCount As Integer = str.Count
Dim intSent As Integer = 1
Dim singleChar As Char

For i = 1开发者_如何学JAVA To maxCount
    singleChar = str.Chars(i)          ' Getting an error here
    If singleChar = "." Then
        intSent = intSent + 1
    End If
Next

MsgBox("Number of sentence = " & intSent)

how to find the number sentences in a string


Edit 2: set Dim maxCount As Integer = str.Count-1. by the same notation as mentioned below, since the array starts at 0, it ends at str.Count-1. So the last element you want to check str.Count-1, not str.Count.

Edit: I believe you error at singleChar = str.Chars(i) is because you set i=1. Unless I am mistaken, arrays initialize at '0' in BASIC (which is all a string is). So the first character is really at str.Chars(0) not str.Char(1). Also, when you get an error, you should post the error in your question. Makes it a lot easier for us to understand and help you with your problem =)

Here. That's how you examine each character of a string. You'll need to use a loop to walk through and examine each element of the string, comparing it to some pre-defined value(s)(pre-defined by you) that indicate the start of (or end of) a sentence.


Parse the String data, using whatever you decide terminates a "sentence" as a splitter (probably multiple characters, such as a period or an exclamation mark), and then count the segments in the returned array.

EDIT: Learn how to use regular expressions, in VB.net, via the following link:

And then use this regular expression:

@"(\S.+?[.!?])(?=\s+|$)"

As demoed in C#, here


If all you're doing is counting periods, you could just use

str.Split(New Char() {"."c}).Length -1

0

精彩评论

暂无评论...
验证码 换一张
取 消