Results 1 to 13 of 13

Thread: [RESOLVED] Character removal

  1. #1

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

    Resolved [RESOLVED] Character removal

    Hi all

    Im stuck on a bit of code and looking for a bit of help.
    I need (i think) a for loop to analyse a string and remove all non alpha characters from it.

    Something like:
    Input text "today is my 21st birthday"
    result "todayismustbirthday"

    I assume Id need a loop to look at each character starting at index 0 and steping by 1 each time.
    The loop would then take the value of all the characters (alpha) and write them to a result string.

    Can anyone help?

    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Character removal

    Firstly, the CodeBank forums are for sharing code samples, not for asking questions. I've asked the mods to move this thread to the appropriate forum.

    As for the question, there are various specific ways it could be done and this is just one. You can treat the original String basically as a Char array and loop through it with For Each loop. You can use the Char.IsLetter method to pick out the letters and add just them to another String. At the end of the loop, that other String will contain just the letters from the first String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

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

    Re: Character removal

    Thanks Jmcilhinnery

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Character removal

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Character removal

    Is this homework by any chance ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

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

    Re: Character removal

    Some past paper questions. Leading to an exam next summer.
    I dont have the soultions part of the paper.
    Have you any different approach to the solution other than the others mentioned.
    I'd like to know a few different solutions for each problem.

  7. #7

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

    Re: Character removal

    Some past paper questions. Leading to an exam next summer.
    I dont have the soultions part of the paper.
    Have you any different approach to the solution other than the others mentioned.
    I'd like to know a few different solutions for each problem.

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

    Re: Character removal

    This is the same as

    http://www.vbforums.com/showthread.php?702009-Methods

    Where you have been given two suggestions already.

  9. #9

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

    Re: Character removal

    Thanks for all the comments

    Figured it out in the end and even got it running in a console(big step for me)

    Dim result As String
    Dim input As String
    result = ""
    input = "Happy Birthday!"

    For i As Integer = 0 To input.Length - 1 Step 1
    If Char.IsLetter(input(i)) Then
    Convert.ToString(input(i))
    result = result + input(i)
    End If
    Next
    result = result.ToLower
    Console.WriteLine(result)

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

    Re: Character removal

    Quote Originally Posted by vbiskillingme View Post
    Thanks for all the comments

    Figured it out in the end and even got it running in a console(big step for me)

    Dim result As String
    Dim input As String
    result = ""
    input = "Happy Birthday!"

    For i As Integer = 0 To input.Length - 1 Step 1
    If Char.IsLetter(input(i)) Then
    Convert.ToString(input(i))
    result = result + input(i)
    End If
    Next
    result = result.ToLower
    Console.WriteLine(result)
    Pretty much what was given to you by .paul. here http://www.vbforums.com/showthread.p...=1#post4297785

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Character removal

    A generalized approach that allows for more discretion in what is kept.

    Code:
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim inpstring As String = "Today is my 21st birthday!"
            Dim result As String = certainCharsOnly(inpstring, True)
        End Sub
    
        Dim keepThese As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    
        Private Function certainCharsOnly(inp As String, Optional lower As Boolean = False) As String
            Dim rv As String = ""
            For Each c As Char In inp
                If keepThese.IndexOf(c) <> -1 Then
                    rv &= c
                End If
            Next
            If lower Then Return rv.ToLower Else Return rv
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: [RESOLVED] Character removal

    Or simply a one liner as shown here which if needed you could add things like period etc. too.

  13. #13

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

    Re: [RESOLVED] Character removal

    Thanks Guys

    I'll give each of them a try and step throught the code to see how each one works.

Tags for this Thread

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