简体   繁体   中英

Replace a part of string with another

The problem

I have two strings s1 and s2

string s1 = " characters of a string, creating a new string object. An array  U.A.E of characters is passed to this method to U.A.E specify U.A.E  the characters to be removed. The order of the elements in the character array does not affect the trim operation.";

string s2 =  " An array  UAE of characters is passed to this method to UAE specify UAE " ;

I am showing the string s1 in a label . and want to bold the s2 part in s1 .

ie , label1.Text = " characters of a string, creating a new string object. <b> An array UAE of characters is passed to this method to UAE specify UAE </b> the characters to be removed. The order of the elements in the character array does not affect the trim operation.";

I can do it by putting <b> in start and </b> end of s2 . But there is dots ( ' . ' ) in UAE of s1 but in s2 its UAE .

What i have tried .

I tried to get first index and last index of words to be replaced .

 int x = s1.Replace(".", "").IndexOf(s2);

But failed to get last index , as UAE repeats .

i tried to put <b> </b> for all words in s2 separately. But the words may repeat in s1 .

I Want to know if there is any string function ,By which i can replace the s2 part of s1 with out the unwanted dots

Note : UAE is just an example , The dots may come in any words

For a general solution for all strings that may or may not contain additional . characters, you need to construct a regex on the fly based on the second string:

var s1 = "Here is a U.A.E string";
var s2 = "UAE string";

//Find all the characters that need an optional . after them
var r = new Regex(@"(\S)(?=\S)");

//Perform a replace on s2 to create a new regex with optional dots, surrounded by () so we capture it
var r2 = new Regex("(" + r.Replace(s2, @"$1\.?") + ")");
//r2 is now a regex containing "(U\.?A\.?E s\.?t\.?r\.?i\.?n\.?g)"

//Use that regex to perform the actual replace, using the captured group to reinsert
var replacedString = r2.Replace(s1, "<b>$1</b>");
//replacedString contains "Here is a <b>U.A.E string</b>"

the same thing with a simpler RegEx

string s1 = " characters of a string, creating a new string object. An array  U.A.E of characters is passed to this method to U.A.E specify U.A.E  the characters to be removed. The order of the elements in the character array does not affect the trim operation.";
string s2 = " An array  UAE of characters is passed to this method to UAE specify UAE ";        

// regex will ignore any dot in tested string while searching pattern
var regex = new Regex(String.Join(@"\.*", s2.AsEnumerable()));              

var result = regex.Replace(s1, m => "<b>" + m.ToString() + "<b>");

You could try something like this

string s1 = " characters of a string, creating a new string object. An array  U.A.E of characters is passed to this method to U.A.E specify U.A.E  the characters to be removed. The order of the elements in the character array does not affect the trim operation.";

string s2 = " An array  UAE of characters is passed to this method to UAE specify UAE ";

s1 = s1.Replace("U.A.E", "UAE");

s1 = s1.Replace(s2, "<b>"+s2+"</b>");

If you want to replace "UAE", with or without dots inbetween the characters, you can use

s1 = Regex.Replace(s1, @"(U\.?A\.?E\.?)", "<b>$1</b>"); 

The \\. searches for a literal . (without the \\ it would match any character). The ? means that the dot is optional.

The brackets are uses to group the matched substring, so that the $1 in the replacement string can insert that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM