|
-
Dec 9th, 2012, 10:39 AM
#1
Thread Starter
New Member
Methods
Hi all
First post and very new to VB.
Trying to finish the construction of a method (design code) before it is implemented in VB.
The method should removes all non-letter characters from an input string and convert the remaining characters to lower case.
Anyone able to help?
thanks
Method Casechange(input As String) As String
’Preconditions: none
’Postconditions: A string is returned consisting of all the letter
’characters from input in the same order and with the same
’multiplicity, but in lower case. Example, input is
’"Happy Birthday!", then "happybirthday" is returned.
result As String
hold As Char
Set result To ""
For i As Integer From 0 To ’Add expression here.
’The loop body goes here, the For loop should add all the letter
’characters in input to result.
End For
’The line of code to convert result to lower case goes here.
Return result
EndMethod
-
Dec 9th, 2012, 10:49 AM
#2
Re: Methods
something like this:
Code:
For i As Integer = 0 To Input.length - 1
If Not Char.IsLetter(Input.Chars(i)) Then
Input = Input.Remove(i, 1)
i -= 1
End If
Next
result = Input.ToLower
Return result
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 9th, 2012, 10:53 AM
#3
Thread Starter
New Member
Re: Methods
Thanks .paul.
I'll give this a try. I just about had it myself looking at your post but I'm new to this and I think I had the odd bracket or something in the wrong place.
Again thanks for such a quick reply.
-
Dec 10th, 2012, 09:30 AM
#4
Re: Methods
Hello, the following might be of interest which is normal code setup as a language extention method which can be used as a normal function on String type. So we can use it against the Text property of a Text box or a string variable for instance.
What .paul. provided is an excellent method, both will do what you want, I am simply providing another idea.
Code:
Public Class Form1
Private Sub Button1_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles Button1.Click
TextBox2.Text = TextBox1.Text.OnlyLetters
End Sub
Private Sub Form1_Load( _
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles MyBase.Load
TextBox1.Text = "Happy Birthday!"
End Sub
Private Sub Button2_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles Button2.Click
Dim Items As New List(Of String) From
{
"A1B2c3d4...",
"1234",
".a."
}
Dim sb As New System.Text.StringBuilder
Dim ThisItem As String = ""
For Each item In Items
ThisItem = item.OnlyLetters
sb.AppendLine(
IIf(String.IsNullOrWhiteSpace(ThisItem),
"(Empty)",
ThisItem).ToString
)
Next
MessageBox.Show(sb.ToString)
End Sub
End Class
Place the following in a code module not a form or class.
Code:
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function OnlyLetters(ByVal sender As String) As String
Return System.Text.RegularExpressions.Regex.Replace(sender, "[^A-Za-z]", "").ToLower
End Function
-
Dec 10th, 2012, 10:08 AM
#5
Thread Starter
New Member
Re: Methods
Thanks kevininstructor
I'm happy to take a look at all the options available. Only way I'll learn.
thanks
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
|