|
-
Dec 10th, 2012, 06:18 AM
#1
Thread Starter
New Member
[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
-
Dec 10th, 2012, 06:38 AM
#2
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.
-
Dec 10th, 2012, 06:48 AM
#3
Thread Starter
New Member
-
Dec 10th, 2012, 09:40 AM
#4
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
-
Dec 10th, 2012, 10:35 AM
#5
Re: Character removal
Is this homework by any chance ?
-
Dec 10th, 2012, 03:08 PM
#6
Thread Starter
New Member
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.
-
Dec 10th, 2012, 03:10 PM
#7
Thread Starter
New Member
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.
-
Dec 10th, 2012, 03:15 PM
#8
Re: Character removal
This is the same as
http://www.vbforums.com/showthread.php?702009-Methods
Where you have been given two suggestions already.
-
Dec 13th, 2012, 09:59 AM
#9
Thread Starter
New Member
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)
-
Dec 13th, 2012, 10:36 AM
#10
Re: Character removal
 Originally Posted by vbiskillingme
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
-
Dec 13th, 2012, 12:50 PM
#11
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
-
Dec 13th, 2012, 12:54 PM
#12
Re: [RESOLVED] Character removal
Or simply a one liner as shown here which if needed you could add things like period etc. too.
-
Dec 14th, 2012, 04:16 AM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|