Solved

replace <input> in string

Posted on 2013-01-13
6
215 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
  • 4
  • 2
6 Comments
 
LVL 74

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 74

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 74

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# System Cannot Find File Specified Error 4 55
asp.net mvc5 3 37
Data Saving 5 28
XML & .net 5 18
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now