Results 1 to 6 of 6

Thread: [RESOLVED] Separateing strings

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    178

    Resolved [RESOLVED] Separateing strings

    I need to separate strings, however not looking to use arrays or list to so if I don't have to.

    I want to separate by a specific character ("-"). Everything beyond and including the - I want to drop or delete.

    So in other words ABC-12 becomes ABC and -12 will be deleted.

    What is the best was to do this?

    I know about split but that seems overkill for what I want to do.

    Thank You

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Separateing strings

    The simplest option would be to use String.Split and just take the first element from the resulting array. The alternative would be use String.IndexOf to find the dash and then String.Substring to get the substring before that index. For such little data, the former is not really overkill.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Separateing strings

    I should also mention that you could use a Regex. I'm not very proficient in that area so I won't try to provide the appropriate pattern but, if you want overkill, that's probably the way to go.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Separateing strings

    Code:
    Dim newString as String = oldString.SubString(0, oldString.indexOf(“-“))

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    178

    Re: Separateing strings

    Quote Originally Posted by jmcilhinney View Post
    The simplest option would be to use String.Split and just take the first element from the resulting array. The alternative would be use String.IndexOf to find the dash and then String.Substring to get the substring before that index. For such little data, the former is not really overkill.
    Thanks I went with the substring method.

    I just meant that using a list, array etc would be overkill, especially in resources, given that part of it was being discarded.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    178

    Resolved Re: Separateing strings

    Quote Originally Posted by .paul. View Post
    Code:
    Dim newString as String = oldString.SubString(0, oldString.indexOf(“-“))
    That is the route I went.

    Thanks.

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