Solved

replace <input> in string

Posted on 2013-01-13
6
219 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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