Results 1 to 5 of 5

Thread: Methods

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Location
    Ireland
    Posts
    9

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Location
    Ireland
    Posts
    9

    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.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Location
    Ireland
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width