none
How to get index in string properly, when i use '\t' tab character instead of space? RRS feed

  • Question


  • I am developing an app to handle string input. But i am failing in below case. 

    I am trying to customize the number of spaces when Tab key is pressed. Here i handling KeyDown event to capture "Tab Key" and inserting required number of spaces. 

    Here, i am maintaining two string property, one for my evaluation and one for display. 

    In display string variable, i will insert the number of spaces, when tab key is pressed. 

    In evaluation string variable, i am adding "\t". Here is my code snippet.

    **Code Snippet**

        int count = EvaluationString.Split('\t').Length - 1;
        int index = currentCursorIndex - (count * 2);
        EvaluationString= InsertText(EvaluationString, "\t", index);


        DisplayString = InsertText(DisplayString , numberofspaces, currentCursorIndex);

    I am stuck in here, i need to insert the "\t" to EvaluationString in exactly, where i am inserting spaces in DisplayString based on its cursor index. But here i need to proceed this based on the Cursor index in DisplayString. 

    I used above codes, but it is failing when i have large number "\t" characted is added. 

    Can anyone suggest generic solution to calculate the cursor index properly?

    MSDN ALERTS

    Friday, November 10, 2017 12:35 PM

Answers

  • Seems that you have a string and want to implement “Insert Tabs”, “Cursor Through Tabs” and “Virtual Spaces” features. Then check this function:

    static Tuple<string, int> Insert( string evaluationString, char charToInsert, int cursorIndex, int tabSize )
    {
       int k = 0;
       int i = 0;
    
       for( ; i < evaluationString.Length; ++i )
       {
          char c = evaluationString[i];
    
          if( c != '\t' )
          {
             if( k == cursorIndex )
             {
                break;
             }
             else
             {
                ++k;
             }
          }
          else
          {
             if( k == cursorIndex )
             {
                break;
             }
             else
             {
                int spaces = ( k / tabSize + 1 ) * tabSize - k;
    
                if( cursorIndex < k + spaces )
                {
                   if( charToInsert != '\t' )
                   {
                      string es = evaluationString.Insert( i, new string( ' ', cursorIndex - k ) + charToInsert );
                      int ci = cursorIndex + 1;
    
                      return new Tuple<string, int>( es, ci );
                   }
                   else
                   {
                      string es = evaluationString.Insert( i, "\t" );
                      int ci = k + spaces;
    
                      return new Tuple<string, int>( es, ci );
                   }
                }
                else
                {
                   k += spaces;
                }
             }
          }
       }
    
       {
          if( cursorIndex > k )
          {
             evaluationString += new string( ' ', cursorIndex - k );
             i += cursorIndex - k;
             k = cursorIndex;
          }
          int spaces = ( k / tabSize + 1 ) * tabSize - k;
          string es = evaluationString.Insert( i, charToInsert.ToString() );
          int ci = k + ( charToInsert == '\t' ? spaces : 1 );
    
          return new Tuple<string, int>( es, ci );
       }
    }
    

     

    The evalualtionString parameter contains the single-line string with original tabs. The charToInsert character is inserted at cursorIndex virtual position, taking into consideration the tabSize. It returns the new string and new position.

    The new DisplayString can be re-calculated based on new EvaluationString.

    Sunday, November 12, 2017 6:45 PM

All replies

  • What is wrong with String.Replace()???

    Friday, November 10, 2017 12:37 PM
  • I already have many spaces, so that will modify those available spaces as "\t" and it will affect my code flow. So, i need to do this only for my tab key press

    MSDN ALERTS

    Friday, November 10, 2017 12:39 PM
  • Seems that you have a string and want to implement “Insert Tabs”, “Cursor Through Tabs” and “Virtual Spaces” features. Then check this function:

    static Tuple<string, int> Insert( string evaluationString, char charToInsert, int cursorIndex, int tabSize )
    {
       int k = 0;
       int i = 0;
    
       for( ; i < evaluationString.Length; ++i )
       {
          char c = evaluationString[i];
    
          if( c != '\t' )
          {
             if( k == cursorIndex )
             {
                break;
             }
             else
             {
                ++k;
             }
          }
          else
          {
             if( k == cursorIndex )
             {
                break;
             }
             else
             {
                int spaces = ( k / tabSize + 1 ) * tabSize - k;
    
                if( cursorIndex < k + spaces )
                {
                   if( charToInsert != '\t' )
                   {
                      string es = evaluationString.Insert( i, new string( ' ', cursorIndex - k ) + charToInsert );
                      int ci = cursorIndex + 1;
    
                      return new Tuple<string, int>( es, ci );
                   }
                   else
                   {
                      string es = evaluationString.Insert( i, "\t" );
                      int ci = k + spaces;
    
                      return new Tuple<string, int>( es, ci );
                   }
                }
                else
                {
                   k += spaces;
                }
             }
          }
       }
    
       {
          if( cursorIndex > k )
          {
             evaluationString += new string( ' ', cursorIndex - k );
             i += cursorIndex - k;
             k = cursorIndex;
          }
          int spaces = ( k / tabSize + 1 ) * tabSize - k;
          string es = evaluationString.Insert( i, charToInsert.ToString() );
          int ci = k + ( charToInsert == '\t' ? spaces : 1 );
    
          return new Tuple<string, int>( es, ci );
       }
    }
    

     

    The evalualtionString parameter contains the single-line string with original tabs. The charToInsert character is inserted at cursorIndex virtual position, taking into consideration the tabSize. It returns the new string and new position.

    The new DisplayString can be re-calculated based on new EvaluationString.

    Sunday, November 12, 2017 6:45 PM
  • This was helpful, Please tell me, how can i detect if cursor index in /t [tab space region]. I am getting index, but it is not retrieved properly for all cases.

    So here, i need calculation from Display String, by imagining as like Evaluation String. 

    Otherwise

    Is there any other option for maintaining added tab space as single space, like the way in Visual Studio Editor. 

    Screenshot

    


    MSDN ALERTS



    • Edited by Ravindran S Tuesday, November 21, 2017 4:08 AM
    Tuesday, November 21, 2017 4:02 AM