Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 44
  • Last Modified:

Separate a number and letter from a string

Hello,

How to seperate number and letter from a string.

Example
String =11QAR
Seperate 11 and QAR

Cheers
0
RIAS
Asked:
RIAS
  • 12
  • 9
  • 4
1 Solution
 
Ryan ChongCommented:
How to seperate number and letter from a string.
if it's fixed length, you can do it easily using Substring function.
0
 
Pawan KumarDatabase ExpertCommented:
Regex-

Regex.Replace("11QAR", "(?:[0-9]+\.?[0-9]*|\.[0-9]+)", "")

Regex.Replace("11QAR", "[^\A-Z]", "")
1
 
RIASAuthor Commented:
Can I write
Dim str as string

str = Regex.Replace("11QAR", "[^\A-Z]", "")
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
RIASAuthor Commented:
Any imports are necessary?
0
 
Pawan KumarDatabase ExpertCommented:
Ok, Try

Dim Pawan As Match = Regex.Match("11QAR", "^([A-Z]+)([0-9]+)$")

If (Pawan.Success) Then

   Console.WriteLine(Pawan.Groups(1).Value)
   Console.WriteLine(Pawan.Groups(2).Value)
   
End If
0
 
RIASAuthor Commented:
Is this import required ?
Imports System.Text.RegularExpressions
0
 
Pawan KumarDatabase ExpertCommented:
Yes please.
0
 
Ryan ChongCommented:
if using Substring, you can try:
Dim myString As String = "11QAR"
        Dim String1 As String = myString.Substring(0, 2)
        Dim String2 As String = myString.Substring(2)
        MessageBox.Show(String.Format("String1 = {0}, String2 = {1}", String1, String2))

Open in new window

0
 
RIASAuthor Commented:
Nope ,Pawan  it doesnt work.
It does not hit the
If (Pawan.Success) Then
0
 
RIASAuthor Commented:
Ryan,

"11QAR" is not fixed ..it can 1111g, 24254545466565y

Cheers
0
 
Ryan ChongCommented:
for Pawan's example,

you should use:

Dim Pawan As Match = Regex.Match("11QAR", "^([0-9]+)([A-Z]+)$")

instead.
0
 
RIASAuthor Commented:
Its returning empty string
0
 
Pawan KumarDatabase ExpertCommented:
Try this

Console.WriteLine(Regex.Match("PA24", "\d+$").Value)          
Console.WriteLine(Regex.Replace("PA24", "\d+$", ""))


O/p

24
PA
0
 
RIASAuthor Commented:
Pawan,

Does not work for
fOR
Console.WriteLine(Regex.Match("24PA", "\d+$").Value)
0
 
RIASAuthor Commented:
my string starts with numbers
0
 
Pawan KumarDatabase ExpertCommented:
Yes got checking..
0
 
Pawan KumarDatabase ExpertCommented:
Try First  - Characters first..

Dim match1 = Regex.Replace("sdff45hg589>@#DF456&<jk778P&&FHJ75", "\D", "")
Dim match2 = Regex.Replace("sdff45hg589>@#DF456&<jk778P&&FHJ75", "[0-9]", "")
           
Console.WriteLine(match1)  
Console.WriteLine(match2)

Output

4558945677875
sdffhg>@#DF&<jkP&&FHJ

Second -- Numbers First..


Dim match11 = Regex.Replace("326487326423sdff45hg589>@#DF456&<jk778P&&FHJ75", "\D", "")
Dim match12 = Regex.Replace("23423432432432sdff45hg589>@#DF456&<jk778P&&FHJ75", "[0-9]", "")
           
Console.WriteLine(match11)  
Console.WriteLine(match12)

Output

3264873264234558945677875
sdffhg>@#DF&<jkP&&FHJ

Hope it helps !!
0
 
RIASAuthor Commented:
Thanks Pawan, really appreciate your help!
0
 
Pawan KumarDatabase ExpertCommented:
Welcome RIAS. !!
0
 
RIASAuthor Commented:
Pawan,
Are you from Pune?
0
 
Pawan KumarDatabase ExpertCommented:
No, I am from Gurgaon. Earlier I was in Pune <<Till 2013>>
0
 
RIASAuthor Commented:
Oh ok , I am from Pune nice to interact with you.

Cheers
0
 
Pawan KumarDatabase ExpertCommented:
Same here bro ! Cheers..
0
 
Ryan ChongCommented:
I think you can try this as well
Private Sub Test(myString As String)
        Dim Test As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(myString, "^([0-9]+)([A-Za-z]+)$")

        If (Test.Success) Then
            Dim String1 As String = Test.Groups(1).Value
            Dim String2 As String = Test.Groups(2).Value
            MessageBox.Show(String.Format("String1 = {0}, String2 = {1}", String1, String2))
        End If
    End Sub

Open in new window

then:
Test("11QAR")
        Test("1111g")
        Test("24254545466565y")

Open in new window


technically speaking, Regex.Replace may not work if you got value like: 1111g222
from:
Dim match1 = Regex.Replace("1111g222", "\D", "")

Open in new window

you will get 1111g222 but not 1111. it returns with "wrong" value. Having said that, if that value: 1111g222 is not possible then it really doesn't matter to you and hence solution using Regex.Replace will still work for you.
1
 
RIASAuthor Commented:
Thanks Ryan Chong!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now