65.9K
CodeProject is changing. Read more.
Home

Class QMString for Processing Strings with Quotes

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Nov 29, 2022

MIT

1 min read

viewsIcon

4012

downloadIcon

50

Internals, code of using QMString

Introduction

Class QMString is the class for processing string with quotation marks. It contains the following functions:

  • CreateStringsInQuotationMarks
  • GetContents
  • Split

This list may be expanded with functions such as Replace, IndexOf, etc. Any method uses the data created by function StringsInQuotationMarks in the same way.

Let's look at the example with function QMString.GetContents and how it works. This function returns substring between characters begin and end.

The simplest case does not cause problems:

      char begin = '(';
      char end   = ')';
      string str = "###(Hello World)@@@";
      int indBegin = str.IndexOf(begin);
      int indEnd   = str.IndexOf(end);
      string result = str.Substring(indBegin + 1, indEnd - indBegin - 1);    

But it does not work in the following cases:

      string str = "###(Hello" (World)" )@@@";
      string str = "###(Hello (World) )@@@";    

Function QMString.GetContents calls function CreateStringsInQuotationMarks which does the following:

  • Searches in input string all quoted substrings and saves them in List StringsInQuotationMarks.
          input string (example):     ("1\"2)3") zxc (Hello)
          StringsInQuotationMarks[0]: "1\"2)3"    
  • Replaces every quoted substring in notQMstring on its position in StringsInQuotationMarks surrounded by decorator char ''.
    notQMstring:	 (♥0♥) zxc (Hello)

After that function, QMString.GetContents does the following:

  • Searches in notQMstring substring between begin and paired end characters:
          finded substring:	♥0♥
  • Replaces in finded substring all substrings surrounded by char with position in StringsInQuotationMarks on value from StringsInQuotationMarks and returns result:
          resulted substring:		"1\"2)3"
  • Sets EndBlock on input string index of first chararacter after char end:
          EndBlock = 10

Code Sample

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace TestQMString
{
  internal class Program
  {
    static void Main(string[] args)
    {
      int EndBlock = 0;
      char begin = '(';
      char end = ')';
      string result = "";
      QMString qmstring;

      // test QMString.Split
      qmstring = new QMString();
      string[] tokens = qmstring.Split
     ("\"aaa;bbb\"Hello, World;2022", 0, new char[] { ',', ';' });
      foreach (string s in tokens) 
         Console.WriteLine(s);

      // test QMString.GetContents
      string input = "(\"1\\\"2)3\" ) zxc ( ( hello\"HELLO\")dd)";
      qmstring = new QMString();
      Console.WriteLine("{0}  [{1}]", input, input.Length);
      int offset = 0;
      while (true)
      {
        result = qmstring.GetContents(input, offset, begin, end, ref EndBlock);
        switch (EndBlock)
        {
          case -2:
            Console.WriteLine("Error: [GetContents] 
                    chars 'begin' & 'end' are same characters");
            return;
          case -1:
            Console.WriteLine("Error:[GetContents] char end is absent");
            return;
          case 0:
            Console.WriteLine("End");
            return;
          default:
            Console.WriteLine("{0} [{1}]  [{2}]", result, EndBlock, result.Length);
            offset += EndBlock;
            break;
        }
      }
    }
  }
}

Results:

      "aaa;bbb"Hello
      World
      2022
      
      ("1\"2)3" ) zxc ( ( hello"HELLO")dd)  [36]
      "1\"2)3" [11]  [8]
      ( hello"HELLO")dd [25]  [17]
      End    

Conclusion

Function GetContents is used in project PPL and it is possible to find its tutorial here:

History