Results 1 to 6 of 6

Thread: How to Pick specific portion of a string

  1. #1
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11

    How to Pick specific portion of a string

    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.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    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.

  3. #3
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11
    Quote Originally Posted by jmcilhinney View Post
    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
    Code:
     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.

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    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)

  5. #5
    Slartibartfast is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Sep 2016
    Location
    Livingston, Scotland
    Posts
    1
    Reputation
    0
    Quote Originally Posted by Mazhar View Post
    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.

    Code:
    Dim mystr As String = "A-001/123a/20"
    Dim s() As String = mystr.Split("/"c)
    
    Dim reqString As String = s(1)
    Last edited by Slartibartfast; 09-18-2016 at 5:22 PM.

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    Quote Originally Posted by Slartibartfast View Post
    Hi
    Here is one of many possibilities.

    Code:
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •