Click here to Skip to main content
15,569,845 members
Articles / Programming Languages / C#
Tip/Trick
Posted 28 Nov 2022

Tagged as

Stats

3.2K views
41 downloads
2 bookmarked

Class QMString for Processing Strings with Quotes

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Nov 2022MIT1 min read
Internals, code of using QMString
This tip discusses what QMString class is. You will see the algorithm and an example of using methods with results.

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:

C#
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:

C#
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.
    C#
    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 ''.
    C#
    notQMstring:	 (♥0♥) zxc (Hello)

After that function, QMString.GetContents does the following:

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

Code Sample

C#
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

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
Slovenia Slovenia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --