CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    2

    Question | Remove text from string

    I got a string includes text from a .txt file (string s = File.ReadAllText(TextFilePath));

    From this string I want to remove all the text that is between the chars "--" and "\r\n".

    For example:
    Original: "aaaa \r\n -- abcde \r\n aaaa"
    New: "aaaa \r\n aaaa"

    How can I do it?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Question | Remove text from string

    Code:
    int index = s.IndexOf("--");
    if( s > 0 )
    {
        return s.Substring( 0, index ) + Environment.NewLine;
    }
    else
    {
        return s;
    }
    Last edited by BigEd781; September 12th, 2010 at 04:39 AM.

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Question | Remove text from string

    You could use String's IndexOf method to find -- and \r\n, then use Substr to remove whatever is inbetween them.
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Sep 2010
    Posts
    2

    Re: Question | Remove text from string

    I want you to take another look at my example:

    what you gave me doesnt answer my problem.

    also, what if there is more than one instance of "-- text \r\n"?

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Question | Remove text from string

    Quote Originally Posted by Sarlula View Post
    I want you to take another look at my example:

    what you gave me doesnt answer my problem.

    also, what if there is more than one instance of "-- text \r\n"?
    1. Yes it does.
    2. Well, if there can be multiple instances of '--' then I guess you should have said that and included it in your example, eh?

    Look at String.Split and/or regular expressions.

  6. #6
    Join Date
    Sep 2010
    Posts
    23

    Re: Question | Remove text from string

    Code:
                string Original = "aaaa \r\n -- abcde \r\n aaaa";
                string new_string = Original;
    
                int start = 0;
                int end = 0;
                int len = 0;
    
                do{
                    start = new_string.IndexOf("--", start);
                    if(start == -1)
                        break;
    
                    end = new_string.IndexOf("\r\n", start);
                    if(end == -1)
                        break;
    
                    len = end + 2 - start;
    
                    new_string = new_string.Remove(start, len);
                } while (new_string.IndexOf("\r\n", start) > 0);
                MessageBox.Show(new_string);
    Or

    Code:
                string Original = "aaaa \r\n -- abcde \r\n aaaa";
                string new_string = Original;
    
                for (int a = 0; a < Original.Length; )
                {
                    if (a >= Original.Length)
                        break;
    
                    if (a + 2 == Original.Length)
                    {
                        new_string += Original.Substring(a, 2);
                        break;
                    }
    
                    if (Original.Substring(a, 2) == "--" && Original.IndexOf("\r\n", a) > a)
                        a = Original.IndexOf("\r\n", a) + 2;
                    else
                        new_string += Original.Substring(a++, 1);
                }
    
                MessageBox.Show(new_string);
    I think i prefere the first one.

    Regular expressions is probably 10 times easier.

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

Featured