Results 1 to 4 of 4

Thread: [RESOLVED] Removing a section from a string

Threaded View

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Removing a section from a string

    I have a string which contains comma delimited text and I need to remove a section if it contains the specific substring "sq.ft".

    For example "colour red, size tall, carpeted throughout lower floor, 2000 sq.ft, tiled roof"

    So I need to remove " 2000 sq.ft,"

    I can find the middle of the section of string I need to remove using indexof("sq.ft") and the end but what is the best way to find the start? i.e. the position after the comma at the start of the string section (the last preceding comma before the "sq.ft") I could loop through each character upto the known position and check if it is a char ',' but im sure there must be a better faster way?
    Code:
       protected void GetUnitWithoutSqFt()
            {
                UnitWithoutSqFt = Unit.Model;
    
    
                if (Unit.Model.ToLower().IndexOf("sq.ft") > -1) ;
                {
                    int x = Unit.Model.ToLower().IndexOf("sq.ft");
                    int z = 0;
    
                    int n = Unit.Model.ToLower().IndexOf(',');
    
                    if (n > -1)
                    {
                        for (int i = Unit.Model.ToLower().IndexOf(',');
                            i > -1;
                            i = Unit.Model.ToLower().IndexOf(',', i + 1))
                        {
                            // for loop end when i=-1 ('a' not found)
                            z = i;
                        }
                    }
    
                    int y = 0;
                    y = Unit.Model.ToLower().IndexOf(",", x);
                    if (y == -1)
                    {
                        y = x + 5;
                    }
    
                    int len = y - z;
    
                    if (z > -1 && len > 0)
                        UnitWithoutSqFt = Unit.Model.Remove(z, len);
    
                    //  UnitWithoutSqFt = "z" + z + "y" + y +"n" +n;
                }
            }
    Last edited by FishGuy; Oct 5th, 2015 at 12:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width