CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    1

    Efficiently splitting a string

    Hello,

    I have a string such as "hello I [am posting] a question on [the code] guru forums on [10th July]".

    I need to split the string to be like this:

    hello
    I
    [am posting]
    a
    question
    on
    [the code]
    guru
    forums
    on
    [10th July]

    I could do this manually using looping, assessing each character and adding to an array of strings as I choose but this would be expensive. I originally used Split(' ') but I need the contents of the square brackets to maintain their contents.

    Can anyone think of any efficient way to complete this task? - or will I need to loop and build....

    Thanks

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Efficiently splitting a string

    Yes, loop (it is not expensive). You will find it easier to use List<string> (genetic list) rather than string[]. You can convert from List<string> to string[] if you want (using ToArray), but... don't.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

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

Featured