C# Replace part of a string

2019-05-14 19:16发布

问题:

How can I replace part of a string that has a potentially unknown starting index. For instance, if I had the following strings:

"<sometexthere width='200'>"
"<sometexthere tile='test' width='345'>"

I would be looking to replace the width attibute value that can have an unknown value and as mentioned before an unknown start index.

The I understand that I would somehow have to base this on the following part, which is constant, I just don't quite understand how to achieve this.

width='

回答1:

using System.Text.RegularExpressions;
Regex reg = new Regex(@"width='\d*'");
string newString = reg.Replace(oldString,"width='325'");

This will return a new string with a new width, provided you put a number between the ' ' in the new width field.



回答2:

So far you've got seven answers telling you to do the wrong thing. Do not use regular expressions to do the job of a parser. I am assuming that your string is a hunk of markup. Let's suppose it is HTML. What does your regular expression do with:

<html>
<script>
    var width='100';
</script>
<blah width =
              '200'>
... and so on ...

I'd be willing to bet as much as a dollar that it replaces the JScript code, which it should not, and does not replace the attribute of the blah tag -- it is perfectly legal to have whitespace in an attribute.

If you have to parse a markup language then parse the markup language. Get yourself a parser and use it; that's what parsers are for.



回答3:

Take a look at the Regex class, you can search for the content of the attribute and repalce the value with this class.

Off the cuff Regex.Replace might do the trick:

var newString = Regex.Replace(@".*width='\d'",string.Foramt("width='{0}'",newValue));


回答4:

Use the regular expression

Regex regex = new Regex(@"\b(width)\b\s*=\s*'d+'");

where the \bs indicate that you wish to match a whole word, \s* allows for zero or any number of whitespace charaters and \d+ allows for one or more numeric placeholder. To replace the numeric value you can then use:

int nRepValue = 400;
string strYourXML = "<sometexthere width='200'>";

// Does the string contain the width?
string strNewString = String.Empty;
Match match = regex.Match(strYourXML);
if (match.Success)
    strNewString = 
        regex.Replace(strYourXML, String.Format("match='{0}'", nRepValue.ToString()));
else 
    // Do something else...

Hope this helps.



回答5:

You can use a regular expression (RegEx) to find and replace all the text in single quotes after "width=".



回答6:

You could use a regular expression, like (?<=width=')(\d+)

Example:

var replaced = Regex.Replace("<sometexthere width='200'>", "(?<=width=')(\\d+)", "123");"

replaced is now: <sometexthere width='123'>



回答7:

I would use a Regex.
Something like this to replace the width value with 123456.

string aString = "<sometexthere tile='test' width='345'>";
Regex regex = new Regex("(?<part1>.*width=')(?<part2>\\d+)(?<part3>'.*)");
var replacedString = regex.Replace(aString, "${part1}123456${part3}");


回答8:

Use regular expressions to achieve this:

using System.Text.RegularExpressions;

...

string yourString = "<sometexthere width='200'>";

// updates width value to 300
yourString = Regex.Replace(yourString , "width='[^']+'", width='300');

// replaces width value with height value of 450
yourString = Regex.Replace(yourString , "width='[^']+'", height='450');