none
C# string split: How to split this string?

    Question

  • I have a string:

        STRT.F          8600.00 :   Start Depth

    I dont know how to get each substring: STRT , F, 8600.00 and Start Depth 

    when I use delimitter . and : , string.split method will also split 8600.00 into 8600 and 00,that is not what I hope.

    or if C# have a method just like C function:

          sscanf(buffer, "%*s%f", &Strt)

    pls Give me a idea.

     

     

    Monday, January 23, 2006 6:40 AM

Answers

  • Try this, wrote it out of the head, so there will be some little type error in it but it must do the trick:


    private static void FindChunks( string row )
    {
        String mnem = String.Empty;
        String unit = null;
        String dataType = String.Empty;
        String information = null;

        // Find mnem.
        for(int i = 0; i < row.Lenght; i++)
        {
            if( row[ i ] != '.' )
            {
                // A delimiter char is found.
                // Remove mnem from the row.
                row = row.SubString( i+1 );
                break;
            }
            else
            {
                // No delimiter char is found.
                // Just append the char.
                mnem += row[ i ].ToString();
            }
        }
       
        // Find unit
        if( row[ 0 ] != " " )
        {
            // Set unit and remove it
            // from the row.
            unit = row[ 0 ].ToString();
            row = row.SubString( 1 );
        }
        else
        {
            // Chunk two is not found.
        }
       
        // Find dataType
        for(int i = 0; i < row.Lenght; i++)
        {
            if( row[ i ] != ':' )
            {
                // A delimiter char is found.
                // Remove mnem from the row.
                row = row.SubString( i+1 );
                break;
            }
            else
            {
                // No delimiter char is found.
                // Just append the char.
                dataType += row[ i ].ToString();
            }
        }
       
        dataType = dataType.TrimBegin();
        information = row.Trim();
    }

     

    Monday, January 23, 2006 8:07 AM

All replies

  • Do the substring's have a fixed size? As far as i can see you can get the first string by searching for the first '.'. The seconds is a tab? And the third is a ':'.

    If it's allways seperated like this, you can use that as delimiters. Otherwise you have to find a pattern that fits. Please can you give more details?
    Monday, January 23, 2006 6:49 AM
  • The file information are listed in red color;

    #MNEM.UNIT          Data Type :  information

    STRT.F                       8600.0000: Start Depth
     STOP.F                     8800.0000:  Stop Depth
     STEP.F                           1.0000:  Depth Increment
     NULL.                         -999.2500:  No Data Flag
     COMP.                        ANYONE: COMPANY
     WELL.    EAST TX TITE GAS SD:   WELL
     FLD .                                FIELD:   NACADOCHES
     LOC .                        LOCATION:   WILDCAT
     CNTY.                               NACA:   COUNTY
     STAT.                                    TX:   STATE
     CTRY.                                    ....:   COUNTRY
     SRVC.             SCHLUMBERGER:   SERVICE COMPANY
     DATE.                            03/17/85:   LOG DATE
     API .                                          9:   API WELL NUMBER
     UWI .                                       ....:   UNIQUE WELL ID

    Thank you!

     

     

    Monday, January 23, 2006 7:02 AM
  • Try this, wrote it out of the head, so there will be some little type error in it but it must do the trick:


    private static void FindChunks( string row )
    {
        String mnem = String.Empty;
        String unit = null;
        String dataType = String.Empty;
        String information = null;

        // Find mnem.
        for(int i = 0; i < row.Lenght; i++)
        {
            if( row[ i ] != '.' )
            {
                // A delimiter char is found.
                // Remove mnem from the row.
                row = row.SubString( i+1 );
                break;
            }
            else
            {
                // No delimiter char is found.
                // Just append the char.
                mnem += row[ i ].ToString();
            }
        }
       
        // Find unit
        if( row[ 0 ] != " " )
        {
            // Set unit and remove it
            // from the row.
            unit = row[ 0 ].ToString();
            row = row.SubString( 1 );
        }
        else
        {
            // Chunk two is not found.
        }
       
        // Find dataType
        for(int i = 0; i < row.Lenght; i++)
        {
            if( row[ i ] != ':' )
            {
                // A delimiter char is found.
                // Remove mnem from the row.
                row = row.SubString( i+1 );
                break;
            }
            else
            {
                // No delimiter char is found.
                // Just append the char.
                dataType += row[ i ].ToString();
            }
        }
       
        dataType = dataType.TrimBegin();
        information = row.Trim();
    }

     

    Monday, January 23, 2006 8:07 AM