|
-
Sep 21st, 2011, 08:01 PM
#1
Thread Starter
Member
How to determing punctuation...
I am trying to
-nput a sentence. determine if the last letter is punchtuation (ie. period, question mark, exclamation point). if not, display an appropriate message.
-input a sentence. count the spaces and determine the number of words, in that sentence
What if statement would be appropriate in this scenario and please be simple, thanks
-
Sep 21st, 2011, 08:10 PM
#2
Re: How to determing punctuation...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 21st, 2011, 08:10 PM
#3
Thread Starter
Member
Re: How to determing punctuation...
vb Code:
If txtName.Text = "," Then MsgBox("Your last punctuation was" & txtName.Text & "bye") End If If txtName.Text = "." Then MsgBox("Your last punctuation was" & txtName.Text & "bye") End If If txtName.Text = "!" Then MsgBox("Your last punctuation was" & txtName.Text & "bye") End If If txtName.Text = "?" Then MsgBox("Your last punctuation was" & txtName.Text & "bye") End If End Sub
kinda stuck here =/
-
Sep 21st, 2011, 08:11 PM
#4
Thread Starter
Member
Re: How to determing punctuation...
 Originally Posted by .paul.
looks like homework?
Shhhhh ok Im so stuck i've been at it for over two hours now no help =/
-
Sep 21st, 2011, 08:15 PM
#5
Re: How to determing punctuation...
ok. if you want to test if a string ends with a character, you'd use the [string].endswith function
to count spaces you'd split the string on " " & number of spaces would be your array length -1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 21st, 2011, 08:40 PM
#6
Thread Starter
Member
Re: How to determing punctuation...
vb Code:
If [name2].EndsWith(",") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith(".") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith("!") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith(";") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith("?") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith(":") Then MsgBox("Your last punctuation was" & name2) End If If [name2].EndsWith(" ") Then MsgBox("Please have the correct punctuation at the end.")
Ok so now I have it like this how would I make it so then I can make it viewable in a message box ?
Sorry for the dumb questions, kinda new D:
-
Sep 21st, 2011, 08:51 PM
#7
Thread Starter
Member
Re: How to determing punctuation...
I tried it like this, help please how can i make it better.
vb Code:
name3 = [name2].EndsWith("," Or "." Or "?" Or "!" Or ";" Or ":") lblPreview1.Text = name3 If [name2].EndsWith(",") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith(".") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith("!") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith(";") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith("?") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith(":") Then MsgBox("Your last punctuation was" & name3) End If If [name2].EndsWith(" ") Then MsgBox("Please have the correct punctuation at the end.") End If End Sub
-
Sep 21st, 2011, 09:33 PM
#8
Member
Re: How to determing punctuation...
Code:
Dim lastpunc As String = "Your last punctuation was "
With TextBox1.Text
If .EndsWith(".") Then
MessageBox.Show(lastpunc & ".")
ElseIf .EndsWith("!") Then
MessageBox.Show(lastpunc & "!")
ElseIf .EndsWith("?") Then
MessageBox.Show(lastpunc & "?")
Else
MessageBox.Show("No punctuation detected.")
End If
End With
'Count words
Dim text() As String = TextBox1.Text.Split(CChar(" "))
Dim count As Integer = text.GetUpperBound(0) + 1
MessageBox.Show("Word count: " & count)
To answer the question you created a new thread for, it is Text.EndsWith(".") Or Text.EndsWith("!"), etc.
-
Sep 21st, 2011, 09:51 PM
#9
Re: How to determing punctuation...
By obtaining the last char in a string we can determine if the char is a punctuation character by using Char.IsPunctuation.
Code:
Dim Sentences As String() = {"Hello John", "Hello Mary!", "Hello Bob.", "Are you feeling good?"}
For Each Sentence In Sentences
If Sentence.EndsWithPunctuation Then
Console.WriteLine("Your sentence '{0}' ends with punctuation", Sentence)
Else
Console.WriteLine("Your sentence '{0}' does not ends with punctuation", Sentence)
End If
Next
Place in code module
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function EndsWithPunctuation(ByVal sender As String) As Boolean
Return Char.IsPunctuation(CChar(sender.Substring(sender.Length - 1, 1)))
End Function
-
Sep 21st, 2011, 10:32 PM
#10
Re: How to determing punctuation...
 Originally Posted by kevininstructor
Code:
CChar(sender.Substring(sender.Length - 1, 1))
Tut, tut!
Code:
sender(sender.Length - 1)
orBoth assume that there is at least one character in the String, as does yours. This:
Code:
sender.LastOrDefault()
doesn't even do that.
-
Sep 22nd, 2011, 09:59 AM
#11
Thread Starter
Member
Re: How to determing punctuation...
-
Sep 22nd, 2011, 10:44 AM
#12
Re: How to determing punctuation...
 Originally Posted by jmcilhinney
Tut, tut!
Code:
sender(sender.Length - 1)
or Both assume that there is at least one character in the String, as does yours. This:
Code:
sender.LastOrDefault()
doesn't even do that.
Not sure if this deserves a "Tut Tut" as I realize that sender.LastOrDefault() is better (thanks for bringing this up) and should have expressed that there might be a possibility of say an empty string, I am so used to first checking for empty string in such a situation prior to using such a function as EndsWithPunctuation i.e.
So this is one alternate way to work this out
Code:
Dim Sentences As String() = _
{ _
"Hello John", _
"Hello Mary!", _
Nothing, _
"Hello Bob.", _
"", _
"?", _
"Are you feeling good?" _
}
For Each Sentence In Sentences
If Sentence.HasValue Then
If Sentence.EndsWithPunctuation Then
Console.WriteLine("Your sentence '{0}' ends with punctuation", _
Sentence)
Else
Console.WriteLine("Your sentence '{0}' does not ends with punctuation", _
Sentence)
End If
Else
Console.WriteLine("Sentence had no content")
End If
Next
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function HasValue(ByVal sender As String) As Boolean
Return Not String.IsNullOrEmpty(sender)
End Function
Any ways so if using assertion within the extension we would use
Code:
Dim Sentences As String() = _
{ _
"Hello John", _
"Hello Mary!", _
Nothing, _
"Hello Bob.", _
"", _
"?", _
"Are you feeling good?" _
}
For Each Sentence In Sentences
If Sentence.EndsWithPunctuation Then
Console.WriteLine("Your sentence '{0}' ends with punctuation", _
Sentence)
Else
Console.WriteLine("Your sentence '{0}' does not ends with punctuation", _
Sentence)
End If
Next
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function EndsWithPunctuation(ByVal sender As String) As Boolean
Return If(Not String.IsNullOrEmpty(sender), _
Char.IsPunctuation(sender.LastOrDefault), False)
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|