Results 1 to 11 of 11

Thread: Removing Text from Beginning and From Middle of a String

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Removing Text from Beginning and From Middle of a String

    Heres the String I need to remove from:

    Start: 29/09/2008 End: 29/09/2008

    Heres the code that I've been messing with, and I just get myself confused, very quickly

    vb Code:
    1. Dim DRow As DataRow = DS.Tables(0).Rows(.cmbEditEvents.SelectedIndex)
    2.             'Start: 00/00/0000 End: 00/00/0000
    3.             Dim SRemoveLen1 As Integer = DRow.Item("Date (D/M/Y)").ToString.IndexOf(":") + 1
    4.             Dim ERemoveLen1 As Integer = DRow.Item("Date (D/M/Y)").ToString.LastIndexOf(":") + 1
    5.             Dim LenghtofString As Integer = Len(DRow.Item("Date (D/M/Y)"))
    6.             Dim StartRange As String = DRow.Item("Date (D/M/Y)").ToString.Remove(0, SRemoveLen1)
    7.             Dim SRemoveLen2 As Integer = StartRange.IndexOf("E")
    8.             Dim EndRange As String = DRow.Item("Date (D/M/Y)").ToString.Remove(SRemoveLen2, LenghtofString - SRemoveLen2)
    9.             MsgBox(EndRange)

    What I'm trying to do is
    set StartRange to 29/09/2008
    and EndRange to 29/09/2008

    then I can use the .SelectionRange.Start & .SelectionRange.End on the Month Cal to set the Selection Range.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Removing Text from Beginning and From Middle of a String

    use regex:

    vb Code:
    1. Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
    2. Dim rx As New Regex("(?<=Start: )\d{2}\/\d{2}\/\d{4}")
    3. Dim StartRange As String = rx.Match(testStr).Value
    4. rx = New Regex("(?<=End: )\d{2}\/\d{2}\/\d{4}")
    5. Dim EndRange As String = rx.Match(testStr).Value
    6. MsgBox(StartRange & vbCrLf & EndRange)

    don't forget to import regex:

    vb Code:
    1. Imports System.Text.RegularExpressions

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Removing Text from Beginning and From Middle of a String

    Umm I rather not use Regex - I'm getting Data from a Access Database, so I rather use Indexs and Removes Please.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing Text from Beginning and From Middle of a String

    this will work as long as the format is what you showed

    Code:
            Dim seSTR As String = "Start: 29/09/2008 End: 29/09/2008"
            seSTR = seSTR.Replace("  ", " ")
            Dim arraySTR() As String = seSTR.Split(" "c)
            Dim startRng As String = arraySTR(1)
            Dim endRng As String = arraySTR(3)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Removing Text from Beginning and From Middle of a String

    ok. try this then:

    vb Code:
    1. Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
    2. Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
    3. Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
    4. MsgBox(StartRange & vbCrLf & EndRange)

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing Text from Beginning and From Middle of a String

    ya' just gotta love regex.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing Text from Beginning and From Middle of a String

    @wesley - does jmc know about your signature?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Removing Text from Beginning and From Middle of a String

    Don't know, but he helps me out a lot, and I mean a lot, I guess I should remove it. Care to explain

    Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
    Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
    Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
    MsgBox(StartRange & vbCrLf & EndRange)

    Is it possible to remove the Random Number (5, 10, etc...) I like using Variables, since it might change from time to time.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing Text from Beginning and From Middle of a String

    hint - "Start: " is 7 characters


    it would help if you explained exactly what the format of the string

    Start: 29/09/2008 End: 29/09/2008

    is?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Removing Text from Beginning and From Middle of a String

    Quote Originally Posted by Wesley008
    Don't know, but he helps me out a lot, and I mean a lot, I guess I should remove it. Care to explain

    Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
    Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
    Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
    MsgBox(StartRange & vbCrLf & EndRange)

    Is it possible to remove the Random Number (5, 10, etc...) I like using Variables, since it might change from time to time.
    its not a random number. "End: " is 5 characters, "Start: " is 7 characters + the date in that format is always 10 characters.

  11. #11
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Removing Text from Beginning and From Middle of a String

    If the original string is always in the same format, I would remove the IndexOf usage all together:
    Code:
            Dim originalString As String = "Start: 29/09/2008 End: 29/09/2008"
            Dim startString As String = originalString.Substring(7, 10)
            Dim endString As String = originalString.Substring(23, 10)
    If it is not in the same format everytime, I would use regex.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

Posting Permissions

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



Click Here to Expand Forum to Full Width