Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

replace <input> in string

Posted on 2013-01-13
6
Medium Priority
?
226 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

647 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