String question

How to parse the text which I get form the richTextBox1, so I could get all the strings closed by " " and put them to the richTextBox2 I thought it should be easy, but get stuck. So any help will be appreciated.
I'm using switch/case, because I have to add more cases later(numbers, letters, comments and other characters). So here is my code...

richTextBox2.Clear();
string s, rezultat = "", result = "";
s = richTextBox1.Text;
int i = 0, j = 0, start, final;

while (i < s.Length)
{
switch (s[ i ])
{
case '"':
{
//example aaa"abc"ggg"123"rrr"456"ddd
i++;
if (j < 2)
{
j = j + 1;
start = s.IndexOf("\"") + 1;
final = s.IndexOf("\"", start);
s = s.Remove(start, final - start);
}
else { j = 0; }
richTextBox2.Text += " - " + s.Substring(start, final - start) + '\n';

} break;

default:
{
result += s[ i ];
i++;
richTextBox2.Text += "OtherSymbol - " + result + '\n';
result = "";
} break;
}



Answer this question

String question

  • chris27uk

    I have writen this out of the head, but i should do the trick i geus:


    string source = null;
    string destination = null;

    const char seperator = '"';

    int index = source.IndexOf( seperator );

    while( index != -1 )
    {
    int startIndex = index;
    int endIndex = source.IndexOf( seperator, startIndex + 1 );

    if( endIndex != -1 )
    {
    destination += source.Substring( startIndex, endIndex );
    index = source.IndexOf( seperator, endIndex + 1 );
    }
    else
    {
    break;
    }
    }




  • biglc1

    On wish line do you get the exception

    Here is some changed code, i only added some checks but i can see some thing over the head because i wrote it out of the head:


    string source = null;
    string destination = null;

    const char seperator = '"';

    int index = source.IndexOf( seperator );

    while( index != -1 )
    {
    if( (index + 1) >= source.Lenght )
    {
    break;
    }

    int startIndex = index;
    int endIndex = source.IndexOf( seperator, startIndex + 1 );

    if( endIndex != -1 && endIndex < source.Lenght )
    {
    destination += source.Substring( startIndex, endIndex );

    if( (endIndex + 1 ) >= source.Lenght )
    {
    break;
    }

    index = source.IndexOf( seperator, endIndex + 1 );
    }
    else
    {
    break;
    }
    }




  • usman_kec

    I tested your example, but the result was - "index and lenght must be refer to location within the string".
    I' ll try to see if I can solve this problem by myself.

  • String question