Solved

replace <input> in string

Posted on 2013-01-13
6
220 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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