Solved

Separate a number and letter from a string

Posted on 2016-11-04
25
36 Views
Last Modified: 2016-11-04
Hello,

How to seperate number and letter from a string.

Example
String =11QAR
Seperate 11 and QAR

Cheers
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 4
25 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41873713
How to seperate number and letter from a string.
if it's fixed length, you can do it easily using Substring function.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873715
Regex-

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

Regex.Replace("11QAR", "[^\A-Z]", "")
1
 

Author Comment

by:RIAS
ID: 41873722
Can I write
Dim str as string

str = Regex.Replace("11QAR", "[^\A-Z]", "")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RIAS
ID: 41873726
Any imports are necessary?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873727
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
 

Author Comment

by:RIAS
ID: 41873738
Is this import required ?
Imports System.Text.RegularExpressions
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873741
Yes please.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41873747
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
 

Author Comment

by:RIAS
ID: 41873748
Nope ,Pawan  it doesnt work.
It does not hit the
If (Pawan.Success) Then
0
 

Author Comment

by:RIAS
ID: 41873751
Ryan,

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

Cheers
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41873759
for Pawan's example,

you should use:

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

instead.
0
 

Author Comment

by:RIAS
ID: 41873763
Its returning empty string
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873766
Try this

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


O/p

24
PA
0
 

Author Comment

by:RIAS
ID: 41873769
Pawan,

Does not work for
fOR
Console.WriteLine(Regex.Match("24PA", "\d+$").Value)
0
 

Author Comment

by:RIAS
ID: 41873770
my string starts with numbers
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873771
Yes got checking..
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41873776
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
 

Author Closing Comment

by:RIAS
ID: 41873781
Thanks Pawan, really appreciate your help!
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873782
Welcome RIAS. !!
0
 

Author Comment

by:RIAS
ID: 41873783
Pawan,
Are you from Pune?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873785
No, I am from Gurgaon. Earlier I was in Pune <<Till 2013>>
0
 

Author Comment

by:RIAS
ID: 41873787
Oh ok , I am from Pune nice to interact with you.

Cheers
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41873790
Same here bro ! Cheers..
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41873794
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
 

Author Comment

by:RIAS
ID: 41873798
Thanks Ryan Chong!
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web page design problem 3 41
Make borderless form movable by user 2 26
Need a quick overview of Selenium testing 2 35
Datatable / Dates ? 4 30
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question