Results 1 to 11 of 11

Thread: How to cut a string? [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question How to cut a string? [Resolved]

    I have a string of characters.

    Eg. "Microsoft Visual Basic .NET"

    Is there anyway or pre-defined function to cut the string into smaller groups, that will contain only a particular characters of the string?

    Eg: After cutting, it becomes
    "Microsoft" or
    "Visual" or
    "Basic" or
    ".NET"
    Last edited by albertlse; Aug 21st, 2003 at 08:35 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    One easy way would be using Split.
    VB Code:
    1. Dim str as String="Microsoft Visual Basic .NET"
    2. Dim SPstr() as String=str.Split(" ")

    Split is more flexible than just splitting by a charachter.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Easy:
    VB Code:
    1. Dim myString As String
    2.         Dim myArrayOfStrings() As String
    3.         Dim i As Integer
    4.         myString = "Microsoft Visual Basic .NET"
    5.         myArrayOfStrings = myString.Split(" ")
    6.  
    7.         For i = 0 To myArrayOfStrings.GetUpperBound(0)
    8.             MessageBox.Show(myArrayOfStrings(i).ToString())
    9.         Next

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You may have this as third option ! Use StringBuilder as follow :

    VB Code:
    1. Dim Strbuilder As New System.Text.StringBuilder()

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Pirate
    You may have this as third option ! Use StringBuilder as follow :

    VB Code:
    1. Dim Strbuilder As New System.Text.StringBuilder()
    That isn't what he was asking for though...

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith
    That isn't what he was asking for though...
    Well , if you read more about StringBuilder , you will know how much efficient it is . anyways I came up with another way though .
    VB Code:
    1. Dim b As System.Text.RegularExpressions.Regex

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Please dont confuse the guy with Regex. Lets try with simpler ones.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Lunatic3
    Please dont confuse the guy with Regex. Lets try with simpler ones.
    hehe , he's not even responding . We just try to give him as much possible ways since .NET offer them apparently .

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    What I am trying to say is that the code you give:
    Dim Strbuilder As New System.Text.StringBuilder()
    or
    Dim b As System.Text.RegularExpressions.Regex

    doesn't show him how to split a string. As a matter of fact it was confusing to me and I know what they are. It didn't help him is what I was saying. The full code on how to use them would be better, not just a declaration statement.

    The stringbuilder class wouldn't be more efficient for this case. The stringbuilder class would be more efficient if you were building strings because it it allocates more memory when it needs it, and doesn't create a whole new address space in memory and copy the contents over to it when you add text to it.

    When your splitting a string up, there is no way to get around of producing new strings in memory if you want them assigned to variables (or an array). Whether you do it with the string class, stringbuilder class, or through regular expressions, your code will still have to allocate new string variables in memory if your going to assign them to new variables.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith

    The stringbuilder class wouldn't be more efficient for this case. The stringbuilder class would be more efficient if you were building strings because it it allocates more memory when it needs it, and doesn't create a whole new address space in memory and copy the contents over to it when you add text to it.

    When your splitting a string up, there is no way to get around of producing new strings in memory if you want them assigned to variables (or an array). Whether you do it with the string class, stringbuilder class, or through regular expressions, your code will still have to allocate new string variables in memory if your going to assign them to new variables.
    I like this part ! exactly what I'w going to explain earlier .

    about the way I posted the code hmm , I just wanted to give him another option although I'm sure he'll go for the first post though ..lol .

  11. #11

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Smile

    I can SPLIT the string now. Thank you.

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