DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to use Replace() function?

  1. #1
    Bassel Tabbah Guest

    How to use Replace() function?


    I use the Replace() function quite often in my work. But I have the following
    problem: suppose I have this text:

    "The US Government is asking the US Department of State" in a TextBox, and
    in my replacement file I have:

    Find: "US", Replace with: "United States";

    Find: "Department of State", Replace with:"Department of Commerce";

    Find: "The US Department of State", Replace with:"The US Department of Defence".

    Now when the Replace() function replaces "US" first, it will no longer be
    able to find the rest of the replacement strings, since the text will have
    become:
    "The United States Department of State", and so on.

    Is there a function "out there" that will replace bigger strings, "whole
    sentences"' first and then go down to smaller and smaller ones? I do not
    know if this is possible or not. Definitely I am not able to write such a
    function. Can you help? Thank you very much for your time.


  2. #2
    Russ Guest

    Re: How to use Replace() function?


    "Bassel Tabbah" <tabbah@un.org> wrote:
    >
    >I use the Replace() function quite often in my work. But I have the following
    >problem: suppose I have this text:
    >
    >"The US Government is asking the US Department of State" in a TextBox, and
    >in my replacement file I have:
    >
    >Find: "US", Replace with: "United States";
    >
    >Find: "Department of State", Replace with:"Department of Commerce";
    >
    >Find: "The US Department of State", Replace with:"The US Department of Defence".
    >
    >Now when the Replace() function replaces "US" first, it will no longer be
    >able to find the rest of the replacement strings, since the text will have
    >become:
    >"The United States Department of State", and so on.
    >
    >Is there a function "out there" that will replace bigger strings, "whole
    >sentences"' first and then go down to smaller and smaller ones? I do not
    >know if this is possible or not. Definitely I am not able to write such

    a
    >function. Can you help? Thank you very much for your time.
    >


    Bassel,
    I think you would have better luck working in the opposite direction. Search
    for, and replace the largest chunks first. For example, first replace all
    instances of "The US Department of State" with "The US Department of Defense".
    Then replace any remaining instances of "Department of State" with "Department
    of Commerce", then replace all "US" with "United States".

    I think that will give you what you need.
    HTH,
    -Russ.

  3. #3
    Bassel Tabbah Guest

    Re: How to use Replace() function?


    "Russ" <russell.thompson@adlink.com> wrote:
    >
    >"Bassel Tabbah" <tabbah@un.org> wrote:
    >>
    >>I use the Replace() function quite often in my work. But I have the following
    >>problem: suppose I have this text:
    >>
    >>"The US Government is asking the US Department of State" in a TextBox,

    and
    >>in my replacement file I have:
    >>
    >>Find: "US", Replace with: "United States";
    >>
    >>Find: "Department of State", Replace with:"Department of Commerce";
    >>
    >>Find: "The US Department of State", Replace with:"The US Department of

    Defence".
    >>
    >>Now when the Replace() function replaces "US" first, it will no longer

    be
    >>able to find the rest of the replacement strings, since the text will have
    >>become:
    >>"The United States Department of State", and so on.
    >>
    >>Is there a function "out there" that will replace bigger strings, "whole
    >>sentences"' first and then go down to smaller and smaller ones? I do not
    >>know if this is possible or not. Definitely I am not able to write such

    >a
    >>function. Can you help? Thank you very much for your time.
    >>

    >
    >Bassel,
    >I think you would have better luck working in the opposite direction. Search
    >for, and replace the largest chunks first. For example, first replace all
    >instances of "The US Department of State" with "The US Department of Defense".
    > Then replace any remaining instances of "Department of State" with "Department
    >of Commerce", then replace all "US" with "United States".
    >
    >I think that will give you what you need.
    >HTH,
    >-Russ.


    Thank you Russ very much. The problem is that I do not have control over
    the sequence of operations, since my find and replace strings are stored
    in a database and the process is automated. The table in the database is
    sorted descendingly to help with what you suggested, however, this did not
    help much.I don't know if the solution lies in creating any array of smaller
    and bigger "find" strings, and to have the function skip smaller strings
    and replace bigger ones first. The whole thing is complicated and above my
    head and beyond my abilities. Thank you very much for your reply.

  4. #4
    Michael Cole Guest

    Re: How to use Replace() function?


    "Bassel Tabbah" <tabbah@un.org> wrote in message
    news:3b782ce6$1@news.devx.com...
    >
    > I use the Replace() function quite often in my work. But I have the

    following
    > problem: suppose I have this text:
    >
    > "The US Government is asking the US Department of State" in a TextBox, and
    > in my replacement file I have:
    >
    > Find: "US", Replace with: "United States";
    >
    > Find: "Department of State", Replace with:"Department of Commerce";
    >
    > Find: "The US Department of State", Replace with:"The US Department of

    Defence".
    >
    > Now when the Replace() function replaces "US" first, it will no longer be
    > able to find the rest of the replacement strings, since the text will have
    > become:
    > "The United States Department of State", and so on.
    >
    > Is there a function "out there" that will replace bigger strings, "whole
    > sentences"' first and then go down to smaller and smaller ones? I do not
    > know if this is possible or not. Definitely I am not able to write such a
    > function. Can you help? Thank you very much for your time.


    A /non_perfect/ solution is to each search/replace on the text AND on all
    following s/r strings, so that on performing the "US" replace, the s/r
    string of "The US Department of State" would become "The United States
    Department of State".

    This would definitely help, but would NOT produce a perfect result.





  5. #5
    Steve Gomori Guest

    Re: How to use Replace() function?

    Couldn't you add a Priority column to the table, and have the Select do an
    Order By on that column? Then you do have control over the sequence of
    operations.

    >
    > Thank you Russ very much. The problem is that I do not have control over
    > the sequence of operations, since my find and replace strings are stored
    > in a database and the process is automated. The table in the database is
    > sorted descendingly to help with what you suggested, however, this did not
    > help much.I don't know if the solution lies in creating any array of

    smaller
    > and bigger "find" strings, and to have the function skip smaller strings
    > and replace bigger ones first. The whole thing is complicated and above my
    > head and beyond my abilities. Thank you very much for your reply.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center