CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2008
    Location
    India
    Posts
    46

    Exclamation String Split functionality-internally how does it works

    Hi Gurus,

    I want to write a function to split a string without using the c#'s split function..
    Write our own split string function..
    can anybody help me in that regards..
    I jus LOVE non-verbal Communication

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: String Split functionality-internally how does it works

    Generally speaking when your college gives you an assigment you're supposed to do it yourself as then you'll actually learn. If someone just hands you the solution you learn nothing.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: String Split functionality-internally how does it works

    Quote Originally Posted by connect_fc View Post
    Hi Gurus,

    I want to write a function to split a string without using the c#'s split function..
    Write our own split string function..
    can anybody help me in that regards..
    Why do you want to reinvent the wheel??

  4. #4
    Join Date
    May 2010
    Posts
    7

    Re: String Split functionality-internally how does it works

    It assumes non-null input.
    Code:
    static string[] SplitString(string str, char[] seperator)
            {
                if (str == string.Empty)
                {
                    return new string[] { str };
                }
    
                List<char> head = new List<char>();
                List<char> tail = new List<char>();
                bool flag = false;
                for (int i = 0; i < str.Length; i++)
                {
                    if (!flag)
                    {
                        if (seperator.Contains(str[i]))
                        {
                            flag = true;
                        }
                        else
                        {
                            head.Add(str[i]);
                        }
                    }
                    else
                    {
                        tail.Add(str[i]);
                    }
                }
    
                if (!flag && tail.Count == 0)
                {
                    return new string[] { str };
                }
    
                List<string> items = new List<string>();
                items.Add(new string(head.ToArray()));
                foreach (string item in SplitString(new string(tail.ToArray()), seperator))
                {
                    items.Add(item);
                }
    
                return items.ToArray();
            }
    Last edited by Pickels; June 21st, 2010 at 06:01 PM.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String Split functionality-internally how does it works

    Pickels, though you are trying to be helpful, it seems pretty obvious that the OP has been given an assignment in school to write a string split function without using the built in method. So, you have essentially just done his homework for him, which is not something we like to do.

  6. #6
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: String Split functionality-internally how does it works

    A simple look into the String class using Reflector would have given the answer.
    ===============================
    My Blog

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: String Split functionality-internally how does it works

    .................. And, just like that.......... The wheel is reinvented.

    Why oh why do teachers want their students to do this is totally beyond me. And, I'm a trainer as well, and I'd rather jump off a bridge before giving any student this type of worthless assignment!!

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String Split functionality-internally how does it works

    Well, I would argue that it helps people to understand how the magic actually works. You can understand the performance characteristics, where the bottlenecks are, etc. You will likely never implement a hash table from scratch in your career, but it can sometimes be very helpful to understand how they work. Joel Spoelsky said it best in his now famous article, The Law of Leaky Abstractions

    As for the OP, I don't think that he is going to get any of these benefits as he only seems concerned with someone providing an answer, not with learning at all.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: String Split functionality-internally how does it works

    I see your point. But I'd argue that teachers aren't thinking properly anymore, perhaps I'm just old fashioned, but I'd rather jump off a bridge before asking my students to reinvent the wheel. I also like to know how things work, but why try to figure out a workaround for something that works 100&#37; ?

    OK, perhaps I'm just old & old fashioned LOL!

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String Split functionality-internally how does it works

    I think that you are just more practical. This kind of thing will not help 90&#37; of the programmers out there. However, many programmers out there are cargo cultists and don't really know what they are doing. Sure, they may be able to get a website up and running, but ask them to do anymore than that, ask them to debug a performance bottleneck (that may reside in their non-public API, eeek!), and they will fail.

    I never graduated to college, so anything I say can be taken with a grain of salt. I quit college after I realized that they take an entire semester (and a lot of my $$$) to teach me what I can teach myself using Google in about 2 weeks.

  11. #11
    Join Date
    Jun 2010
    Posts
    85

    Re: String Split functionality-internally how does it works

    I disagree,

    I see many students that come that can not do anything if Microsoft does not give them a method for it, and with a straight face expect me to hire them. Ummm if there was a method that did that, I would not need to hire you, I would just call it myself....

    I will even go a step further and state that you can not really understand how to use C Sharp unless you have a very good understanding on pointers and how dynamic memory allocation is really done. Sadly too many just pass by all that in one paragrah which pretty much reads "the garbage collector takes care of it for you". I have seen to many C Sharp apps with the impossible "memory leak" to ever believe that nonesense.

    Long winded way of saying that although kids these days should definatly learn to use the tools that they are given today since they increase production so much, they should also understand them.

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: String Split functionality-internally how does it works

    Quote Originally Posted by HanneSThEGreaT View Post
    .................. And, just like that.......... The wheel is reinvented.

    Why oh why do teachers want their students to do this is totally beyond me. And, I'm a trainer as well, and I'd rather jump off a bridge before giving any student this type of worthless assignment!!
    I'd rather jump off a bridge before I *don't* make students do this kind of assignment. The OP is exactly why I'd give the assignment. He couldn't figure out the wheel. If he can't figure out what you so correctly imply is a trivial and useless task, how the hell could he figure out something more complex like a lockfree threadsafe queue structure?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: String Split functionality-internally how does it works

    Maybe I should change my way of thinking. I see all your valid points you make.

    I think the whole point I have succeeded in making throughout this thread, is that I'm old fashioned

    Hannes

  14. #14
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String Split functionality-internally how does it works

    Well, it's going to come down to the person too. We like programming, we like CS. Therefore we will go above and beyond to learn how stuff works because it drives us. For many though this is not the case and they will do the bare minimum needed to get by. Assignments like these at least forces them to think about this stuff a little. When they are running an IndexOf in a tight loop on a huge document maybe they will know where the problem lies

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