Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an alphanumeric string like sdff45hg589>@#DF456&<jk778P&&FHJ75, of which I want to extract only the numbers, without using regex or any other functions.

Dim input As String = "sdff45hg589>@#DF456&<jk778P&&FHJ75"
Dim output As String = New String((From c As Char In input Select c Where Char.IsDigit(c)).ToArray())
MsgBox(output) 'Display the output 

The message box will display 4558945677875.

For extracting letters from the same string, I use the following:

 Dim input As String =  "sdff45hg589>@#DF456&<jk778P&&FHJ75"
 Dim output As String = New String((From c As Char In input Select c Where Char.IsLetter(c)).ToArray())
 MsgBox (output)

Is this a better way to extract both?

share|improve this question
2  
This appears to be copied from: stackoverflow.com/a/24650229/1305253 –  rolfl Aug 10 '14 at 17:36

2 Answers 2

up vote 3 down vote accepted

If you want to extract both at the same time (I don't know if it is a requirement, it isn't clear in the question) you should iterate once through each character to place them in different lists.

  Dim input As String = "sdff45hg589>@#DF456&<jk778P&&FHJ75"
  Dim characters As List(Of Char) = New List(Of Char)()
  Dim numbers As List(Of Char) = New List(Of Char)()
  For Each c As Char In input
     If (Char.IsNumber(c)) Then
        numbers.Add(c)
     ElseIf (Char.IsLetter(c)) Then
        characters.Add(c)
     End If
  Next

'Do whatever you have to do here

Edit

As pointed out @mjolka, since the goal of the user is to show the splitted numbers and letters, a StringBuilder would be more appropriate. Here is my updated answer :

  Dim input As String = "sdff45hg589>@#DF456&<jk778P&&FHJ75"
  Dim characters As StringBuilder = New StringBuilder()
  Dim numbers As StringBuilder = New StringBuilder()
  For Each c As Char In input
     If (Char.IsNumber(c)) Then
        numbers.Append(c)
     ElseIf (Char.IsLetter(c)) Then
        characters.Append(c)
     End If
  Next

'Do whatever you have to do here
share|improve this answer
    
I think a StringBuilder would be better than a List(Of Char). It makes the intention clearer and you can just call numbers.ToString(). –  mjolka Aug 8 '14 at 23:07
    
That is totally true, I'll update my answer –  TopinFrassi Aug 8 '14 at 23:08

you can do this with a very simple regex replace.

Dim match = Regex.Replace("sdff45hg589>@#DF456&<jk778P&&FHJ75", "\D", "")

non-digits is "\D", and then replace it with empty string.

I ran for above data it works fine.

share|improve this answer
    
But how to extract letters using this? –  Sujith Karivelil Aug 8 '14 at 13:54
1  
use \d to remove digit. –  paritosh Aug 8 '14 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.