Question How to Pick specific portion of a string

Mazhar

Member
Joined
Aug 27, 2016
Messages
15
Programming Experience
Beginner
I have a string like
dim mystr as String= 'A-001/123a/20'

I want to pick "123a" only from mystr using VB.NET

how can I ?

Thanks in advance.
 
Before you can write general code to extract a substring from a string, you have to know what the rules are. I could provide you with ten different code snippets that would do exactly as you asked but they would not work with any other data. You need to explain to us exactly what those rules are. For instance, are you saying that you want the text from a specific character index to another? The text between the first and second slash? The text between the first and last slash? Something else? Computers can't read your mind so you have to give them EXACT instructions. We can't read your mind either.
 
Before you can write general code to extract a substring from a string, you have to know what the rules are. I could provide you with ten different code snippets that would do exactly as you asked but they would not work with any other data. You need to explain to us exactly what those rules are. For instance, are you saying that you want the text from a specific character index to another? The text between the first and second slash? The text between the first and last slash? Something else? Computers can't read your mind so you have to give them EXACT instructions. We can't read your mind either.

Thank u very much for your explanation, Yes exactly I want The text of any length and any type between the first and second slash, I mentioned in quouts "123a", But you are absolutely right it does not clarify that only 123a is required or the text between two slashes is required. sorry for inconvenience.

The code i tried to extract the same
VB.NET:
 Dim MyStr As String = cmbPurBill.Text
                            Dim fstring As String
                            Dim MYBILL As String
                            For x = 7 To Len(MyStr) - 3
                                fstring = Mid(MyStr, x, 1)
                                If Mid(MyStr, x, 1) = "/" Then Exit For
                                MYBILL = MYBILL + fstring
                            Next x
                            TxtBillNo.Text = MYBILL

if u have a better idea please help me.
 
Here's what I would do:
Dim firstSlashIndex = myString.IndexOf("/")
Dim substringStartIndex = firstSlashIndex + 1
Dim secondSlashIndex = myString.IndexOf("/", substringStartIndex)
Dim substringLength = secondSlashIndex - substringStartIndex
Dim mySubstring = myString.Substring(substringStartIndex, substringLength)
 
I have a string like
dim mystr as String= 'A-001/123a/20'

I want to pick "123a" only from mystr using VB.NET

how can I ?

Thanks in advance.

Hi
Here is one of many possibilities.

VB.NET:
Dim mystr As String = "A-001/123a/20"
Dim s() As String = mystr.Split("/"c)

Dim reqString As String = s(1)
 
Last edited:
Hi
Here is one of many possibilities.

VB.NET:
Dim mystr As String = "A-001/123a/20"
Dim s() As String = mystr.Split("/"c)

Dim reqString As String = s(1)

Particularly if there are only likely to be two slashes in the text, that is indeed a good option.
 
Back
Top