Solved

replace <input> in string

Posted on 2013-01-13
6
221 Views
Last Modified: 2013-01-16
Hello,
I have a string:
Here is <input style="width: 100px; height: 22px;" type="text" /> and <input style="width: 100px; height: 22px;" type="text" /> and <input style="width: 100px; height: 22px;" type="text" />.

I would like to replace <input /> with following items: "house; car; ship"
The result should be:
Here is house and car and ship.

How is best to do this?
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38772224
You could do:

string template = "Here is <input style=\"width: 100px; height: 22px;\" type=\"text\" /> and <input style=\"width: 100px; height: 22px;\" type=\"text\" /> and <input style=\"width: 100px; height: 22px;\" type=\"text\" />.";
string placeholder = "<input style=\"width: 100px; height: 22px;\" type=\"text\" />";
string items = "house; car; ship";
string[] replacements = items.Split(';');
string result = template;
int index = result.IndexOf(placeholder);
int currentReplace = 0;
            
while (index >= 0)
{
    result = result.Substring(0, index) + replacements[currentReplace].Trim() + result.Substring(index + placeholder.Length);

    index = result.IndexOf(placeholder);
    currentReplace++;
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38772233
Or slightly more elaborate:

using System.Text.RegularExpressions;

...

string template = "Here is <input style=\"width: 100px; height: 22px;\" type=\"text\" /> and <input style=\"width: 100px; height: 22px;\" type=\"text\" /> and <input style=\"width: 100px; height: 22px;\" type=\"text\" />.";
string placeholder = "<input style=\"width: 100px; height: 22px;\" type=\"text\" />";
string items = "house; car; ship";
string[] replacements = items.Split(';');
int index = 0;

string result = Regex.Replace(template, Regex.Escape(placeholder), delegate(Match m) { return replacements[index++].Trim(); });

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38772240
Do note that in either of the above, it is assumed that the template has the same number of placeholders as the replacement string has values to replace. If these don't match, then you will get an exception.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:johnkainn
ID: 38772737
The problem is that the width and height may differ.
Is it not possible to replace all items that start with <input and end with  /> ?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38772822
In the 2nd snippet I provided, change line 6 to:

string placeholder = "<input [^>]+/>";

Open in new window


...and change line 11 to:

string result = Regex.Replace(template, placeholder, delegate(Match m) { return replacements[index++].Trim(); });

Open in new window

0
 

Author Closing Comment

by:johnkainn
ID: 38782528
Thank you very much:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question