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. General Programming
  3. C#
  4. Splitting strings while taking escaped characters into account

Splitting strings while taking escaped characters into account

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    martin_hughes
    wrote on last edited by
    #1

    I'm attempting to split a file containing records into a number of substrings. The file spec allows for the normal delimiter (') to be escaped within a record by being prefixed with a question mark. Unless there is some trick to it, I guess this rules out using String.Split() so I wrote the following

    static ArrayList DealWithEscapedCharacters(StreamReader inputStream, char segmentTerminator, char escapeCharacter)
    {
    ArrayList results = new ArrayList();

            string tempBuffer = "";
            char previousChar = ' ';
            char currentChar;
    
            while (!inputStream.EndOfStream)
            {
    
                currentChar = (char)inputStream.Read();
                
                if (currentChar == segmentTerminator && previousChar != escapeCharacter)
                {
                    tempBuffer += currentChar;
                    results.Add(tempBuffer);
                    tempBuffer = "";
                }
                else
                {
                    tempBuffer += currentChar;
                }
    
                previousChar = currentChar;
            }
    
            return results;
    
        }
    

    This works, and is very quick even on my laptop. However, can anyone suggest a better/more flexible/even more performant approach?

    Me: Can you see the "up" arrow? User:Errr...ummm....no. Me: Can you see an arrow that points upwards? User: Oh yes, I see it now! -Excerpt from a support call taken by me, 08/31/2007

    L 1 Reply Last reply
    0
    • M martin_hughes

      I'm attempting to split a file containing records into a number of substrings. The file spec allows for the normal delimiter (') to be escaped within a record by being prefixed with a question mark. Unless there is some trick to it, I guess this rules out using String.Split() so I wrote the following

      static ArrayList DealWithEscapedCharacters(StreamReader inputStream, char segmentTerminator, char escapeCharacter)
      {
      ArrayList results = new ArrayList();

              string tempBuffer = "";
              char previousChar = ' ';
              char currentChar;
      
              while (!inputStream.EndOfStream)
              {
      
                  currentChar = (char)inputStream.Read();
                  
                  if (currentChar == segmentTerminator && previousChar != escapeCharacter)
                  {
                      tempBuffer += currentChar;
                      results.Add(tempBuffer);
                      tempBuffer = "";
                  }
                  else
                  {
                      tempBuffer += currentChar;
                  }
      
                  previousChar = currentChar;
              }
      
              return results;
      
          }
      

      This works, and is very quick even on my laptop. However, can anyone suggest a better/more flexible/even more performant approach?

      Me: Can you see the "up" arrow? User:Errr...ummm....no. Me: Can you see an arrow that points upwards? User: Oh yes, I see it now! -Excerpt from a support call taken by me, 08/31/2007

      L Offline
      L Offline
      Larantz
      wrote on last edited by
      #2

      There should be a performance gain in using List<string> as supposed to ArrayList where the strings will be boxed. Larantz-

      for those about to code, we salute you
      http://www.itverket.no

      Please refer to the Forum Guidelines for appropriate posting.

      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