Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Seems familiar?

Seems familiar?

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasequestion
10 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gnjunge
    wrote on last edited by
    #1

    I came across the following function:

    private static string[] SplitByString(string testString, string split)
    {
    int offset = 0;
    int index = 0;
    int[] offsets = new int[testString.Length + 1];

            while (index < testString.Length)
            {
                int indexOf = testString.IndexOf(split, index);
                if (indexOf != -1)
                {
                    offsets\[offset++\] = indexOf;
                    index = (indexOf + split.Length);
                }
                else
                {
                    index = testString.Length;
                }
            }
    
            string\[\] final = new string\[offset + 1\];
            if (offset == 0)
            {
                final\[0\] = testString;
            }
            else
            {
                offset--;
                final\[0\] = testString.Substring(0, offsets\[0\]);
                for (int i = 0; i < offset; i++)
                {
                    final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] 
                                   - offsets\[i\] - split.Length);
                }
                final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length);
            }
            return final;
        }
    

    and just had to run it in order to make sure that this function was really only a rewrite of String.Split(string[] seperator, StringSplitOptions option) which was taken from some website.

    P C 2 Replies Last reply
    0
    • G gnjunge

      I came across the following function:

      private static string[] SplitByString(string testString, string split)
      {
      int offset = 0;
      int index = 0;
      int[] offsets = new int[testString.Length + 1];

              while (index < testString.Length)
              {
                  int indexOf = testString.IndexOf(split, index);
                  if (indexOf != -1)
                  {
                      offsets\[offset++\] = indexOf;
                      index = (indexOf + split.Length);
                  }
                  else
                  {
                      index = testString.Length;
                  }
              }
      
              string\[\] final = new string\[offset + 1\];
              if (offset == 0)
              {
                  final\[0\] = testString;
              }
              else
              {
                  offset--;
                  final\[0\] = testString.Substring(0, offsets\[0\]);
                  for (int i = 0; i < offset; i++)
                  {
                      final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] 
                                     - offsets\[i\] - split.Length);
                  }
                  final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length);
              }
              return final;
          }
      

      and just had to run it in order to make sure that this function was really only a rewrite of String.Split(string[] seperator, StringSplitOptions option) which was taken from some website.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting 12345,"Coyote, Wile E.","\"Super Genius\"" on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel.

      D G G 3 Replies Last reply
      0
      • P PIEBALDconsult

        I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting 12345,"Coyote, Wile E.","\"Super Genius\"" on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        PIEBALDconsult wrote:

        12345,"Coyote, Wile E.","\"Super Genius\""

        If he was that much of a genius, why did he keep buying all this crap from ACME after everything he bought repeatedly failed?? :confused: :-D

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        P 1 Reply Last reply
        0
        • D Dave Kreskowiak

          PIEBALDconsult wrote:

          12345,"Coyote, Wile E.","\"Super Genius\""

          If he was that much of a genius, why did he keep buying all this crap from ACME after everything he bought repeatedly failed?? :confused: :-D

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          They're the only ones to give him a credit card.

          1 Reply Last reply
          0
          • P PIEBALDconsult

            I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting 12345,"Coyote, Wile E.","\"Super Genius\"" on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel.

            G Offline
            G Offline
            gnjunge
            wrote on last edited by
            #5

            I don't know about the original website where it was copied from, but the code I found it in was written/targeted for .NET 3.5.....

            P 1 Reply Last reply
            0
            • P PIEBALDconsult

              I agree that it's not very well written, but when was it written? Splitting on string was added in .net 2.0; this may have been written before that... as was mine. Mine also has the benefit of honoring quotes and escapes within the string, so splitting 12345,"Coyote, Wile E.","\"Super Genius\"" on comma will return the proper results. Reinventing the wheel is acceptable if (and only if) it results in a better wheel.

              G Offline
              G Offline
              Guido_d
              wrote on last edited by
              #6

              string.Split() was available in framework 1.0 and 1.1 too..

              P 1 Reply Last reply
              0
              • G Guido_d

                string.Split() was available in framework 1.0 and 1.1 too..

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                But only with splitting on single-character delimiters, not multi-character (string) delimiters.

                S 1 Reply Last reply
                0
                • G gnjunge

                  I don't know about the original website where it was copied from, but the code I found it in was written/targeted for .NET 3.5.....

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Mark it as Obsolete.

                  1 Reply Last reply
                  0
                  • G gnjunge

                    I came across the following function:

                    private static string[] SplitByString(string testString, string split)
                    {
                    int offset = 0;
                    int index = 0;
                    int[] offsets = new int[testString.Length + 1];

                            while (index < testString.Length)
                            {
                                int indexOf = testString.IndexOf(split, index);
                                if (indexOf != -1)
                                {
                                    offsets\[offset++\] = indexOf;
                                    index = (indexOf + split.Length);
                                }
                                else
                                {
                                    index = testString.Length;
                                }
                            }
                    
                            string\[\] final = new string\[offset + 1\];
                            if (offset == 0)
                            {
                                final\[0\] = testString;
                            }
                            else
                            {
                                offset--;
                                final\[0\] = testString.Substring(0, offsets\[0\]);
                                for (int i = 0; i < offset; i++)
                                {
                                    final\[i + 1\] = testString.Substring(offsets\[i\] + split.Length, offsets\[i + 1\] 
                                                   - offsets\[i\] - split.Length);
                                }
                                final\[offset + 1\] = testString.Substring(offsets\[offset\] + split.Length);
                            }
                            return final;
                        }
                    

                    and just had to run it in order to make sure that this function was really only a rewrite of String.Split(string[] seperator, StringSplitOptions option) which was taken from some website.

                    C Offline
                    C Offline
                    Christoph Menge
                    wrote on last edited by
                    #9

                    Less subtle, and probably posted a thousand times: bool b = whatever; switch(b) { case true: // do something break; case false: // omg... do something else? break; } Seen as PHP-Code... This is completely unrelated, but it just came to my mind...

                    "Obstacles are those frightening things you see when you take your Eyes off your aim" - Henry Ford
                    Articles  Blog

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      But only with splitting on single-character delimiters, not multi-character (string) delimiters.

                      S Offline
                      S Offline
                      Steve Westbrook
                      wrote on last edited by
                      #10

                      The split(string, delim) function from earlier VB versions is still available and accepts multi-character delimiter parameters.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups