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:
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:
After that function, QMString.GetContents
does the following:
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;
qmstring = new QMString();
string[] tokens = qmstring.Split
("\"aaa;bbb\"Hello, World;2022", 0, new char[] { ',', ';' });
foreach (string s in tokens)
Console.WriteLine(s);
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